<?php

namespace App\Models;

use App\Enums\LocationState;
use App\Helpers\DateHelper;
use App\Services\LocationService;
use App\Services\NotificationService;
use PDO;
use Config\Database;
use DateTime;

class Location extends BaseModel
{
    protected static $table = 'locations';

    public $id;
    public $process_id;
    public $group_id;
    public $subgroup_id;
    public $procedure_route_id;
    public $state;
    public $derived_date;
    public $receipt_date;
    public $attended_date;
    public $comment;
    public $deleted_at;

    public function __construct($data = [])
    {
        parent::__construct($data);
    }

    public function save()
    {
        $db = Database::getConnection();

        if ($this->id) {
            $query = "UPDATE locations 
                      SET process_id = :process_id, group_id = :group_id, subgroup_id = :subgroup_id, procedure_route_id = :procedure_route_id, 
                          state = :state, derived_date = :derived_date, receipt_date = :receipt_date, attended_date = :attended_date, comment = :comment, 
                          deleted_at = :deleted_at
                      WHERE id = :id";
            $stmt = $db->prepare($query);
            $stmt->execute([
                ':id' => $this->id,
                ':process_id' => $this->process_id,
                ':group_id' => $this->group_id,
                ':subgroup_id' => $this->subgroup_id,
                ':procedure_route_id' => $this->procedure_route_id,
                ':state' => $this->state,
                ':derived_date' => $this->derived_date,
                ':receipt_date' => $this->receipt_date,
                ':attended_date' => $this->attended_date,
                ':comment' => $this->comment,
                ':deleted_at' => $this->deleted_at,
            ]);
        } else {
            $query = "INSERT INTO locations (process_id, group_id, subgroup_id, procedure_route_id, state, derived_date, receipt_date, attended_date, comment, 
                                             deleted_at) 
                      VALUES (:process_id, :group_id, :subgroup_id, :procedure_route_id, :state, :derived_date, :receipt_date, :attended_date, :comment, 
                              :deleted_at)";
            $stmt = $db->prepare($query);
            $stmt->execute([
                ':process_id' => $this->process_id,
                ':group_id' => $this->group_id,
                ':subgroup_id' => $this->subgroup_id,
                ':procedure_route_id' => $this->procedure_route_id,
                ':state' => $this->state,
                ':derived_date' => $this->derived_date,
                ':receipt_date' => $this->receipt_date,
                ':attended_date' => $this->attended_date,
                ':comment' => $this->comment,
                ':deleted_at' => $this->deleted_at,
            ]);

            $this->id = $db->lastInsertId();
        }
    }

    public function process()
    {
        return Process::find($this->process_id);
    }

    public function group()
    {
        return Group::find($this->group_id);
    }

    public function subgroup()
    {
        return Subgroup::find($this->subgroup_id);
    }

    public function areaGroupType()
    {
        $group = $this->group();
        return $group ? AreaGroupType::find($group->area_group_type_id) : null;
    }

    public function area()
    {
        $areaGroupType = $this->areaGroupType();
        return $areaGroupType ? Area::find($areaGroupType->area_id) : null;
    }

    public function groupType()
    {
        $areaGroupType = $this->areaGroupType();
        return $areaGroupType ? GroupType::find($areaGroupType->group_type_id) : null;
    }

    public function attachments()
    {
        return Attachment::where('location_id', $this->id);
    }
}