package LasTres::Schema::Result::PJKnownPlaces;

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_known_places');

__PACKAGE__->add_columns(
    owner => {
        data_type      => 'uuid',
        is_nullable    => 0,
        is_foreign_key => 1,
    },
    planet => {
        data_type   => 'text',
        is_nullable => 0,
        accessor    => '_planet',
    },
    super_area => {
        data_type   => 'text',
        is_nullable => 0,
        accessor    => '_super_area',
    },
    area => {
        data_type   => 'text',
        is_nullable => 0,
        accessor    => '_area',
    },
    location => {
        data_type   => 'text',
        is_nullable => 0,
        accessor    => '_location',
    },
);

sub location {
    my $self     = shift;
    my $location = shift;
    my $planet;
    my $super_area;
    my $area;
    if ( defined $location ) {
        $self->_location( $location->identifier );
        $area = $location->parent;
        $self->_area( $area->identifier );
        $super_area = $area->parent;
        $self->_super_area( $super_area->identifier );
        $planet = $super_area->parent;
        $self->_planet( $planet->identifier );
    }
    $location   = $self->_location;
    $area       = $self->_area;
    $super_area = $self->_super_area;
    $planet     = $self->_planet;
    $location =
      LasTres::Location::get( $planet, $super_area, $area, $location );
    return $location;
}

__PACKAGE__->set_primary_key(qw/owner planet super_area area location/);
__PACKAGE__->belongs_to( 'owner', 'LasTres::Schema::Result::PJ' );
1;
