<?php

namespace App\Models;

use PDO;
use Config\Database;

class Procedure extends BaseModel
{
    protected static $table = 'procedures';

    public $id;
    public $description;
    public $group_id;
    public $subgroup_id;
    public $time;
    public $modality_id;
    public $active;
    public $deleted_at;
    public $correction_days;

    public function __construct($data = [])
    {
        parent::__construct($data);
        
        $this->active = $data['active'] ?? 1;
        $this->correction_days = $data['correction_days'] ?? 2;

        $lastNode = $this->getSecondRouteNode();
        $this->group_id = $lastNode ? $lastNode->group_id : null;
        $this->subgroup_id = $lastNode ? $lastNode->subgroup_id : null;
    }

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

        if ($this->id) {
            $query = "UPDATE procedures 
                    SET description = :description, 
                        group_id = :group_id, 
                        subgroup_id = :subgroup_id, 
                        time = :time, 
                        modality_id = :modality_id, 
                        active = :active,
                        correction_days = :correction_days 
                    WHERE id = :id";
            $stmt = $db->prepare($query);
            $stmt->execute([
                ':id' => $this->id,
                ':description' => $this->description,
                ':group_id' => $this->group_id,
                ':subgroup_id' => $this->subgroup_id,
                ':time' => $this->time,
                ':modality_id' => $this->modality_id,
                ':active' => $this->active,
                ':correction_days' => $this->correction_days
            ]);
        } else {
            $query = "INSERT INTO procedures (description, group_id, subgroup_id, time, modality_id, active, correction_days) 
                    VALUES (:description, :group_id, :subgroup_id, :time, :modality_id, :active, :correction_days)";
            $stmt = $db->prepare($query);
            $stmt->execute([
                ':description' => $this->description,
                ':group_id' => $this->group_id,
                ':subgroup_id' => $this->subgroup_id,
                ':time' => $this->time,
                ':modality_id' => $this->modality_id,
                ':active' => $this->active,
                ':correction_days' => $this->correction_days
            ]);
            $this->id = $db->lastInsertId();
        }
    }

    public function modality()
    {
        return Modality::find($this->modality_id);
    }

    public function getSecondRouteNode()
    {
        $firstNode = ProcedureRoute::findByMultiple([
            'procedure_id' => $this->id,
            'previous_step_id' => null
        ]);

        if (!$firstNode || !$firstNode->next_step_id) {
            return null;
        }

        return ProcedureRoute::find($firstNode->next_step_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;
    }
}