package LasTres::Schema::Result::PJLog;

use v5.36.0;
use strict;
use warnings;

use feature 'signatures';

use parent 'DBIx::Class::Core';

use Data::Dumper;

use JSON qw/from_json/;
use DateTime::Format::ISO8601::Format;

use Moo;

__PACKAGE__->table('player_pjs_log');

__PACKAGE__->load_components(qw/InflateColumn::DateTime/);
__PACKAGE__->add_columns(
    uuid => {
        data_type   => 'uuid',
        is_nullable => 0,
    },
    content => {
        data_type   => 'jsonb',
        accessor    => '_content',
        is_nullable => 0,
    },
    owner => {
        data_type      => 'uuid',
        is_foreign_key => 1,
        is_nullable    => 0,
    },
    date => {
        data_type     => 'timestamp',
        inflate_datetime => 1,
        default_value => \'NOW()',
        is_nullable   => 0,
    },
);

sub content($self) {
    return from_json($self->_content);
}

__PACKAGE__->set_primary_key('uuid');
__PACKAGE__->belongs_to( 'owner', 'LasTres::Schema::Result::PJ' );

sub hash ($self) {
    my $iso_formatter = DateTime::Format::ISO8601::Format->new(second_precision => 3);
    return {
        uuid    => $self->uuid,
        content => $self->content,
        date    => $iso_formatter->format_datetime($self->date)
    };
}

sub sqlt_deploy_hook ( $self, $sqlt_table ) {
    $sqlt_table->add_index( name => 'index_log', fields => [qw/owner date/] );
}
1;
