<?php

namespace App\Models;

use PDO;
use Config\Database;

class UserUgel extends BaseModel
{
    protected static $table = 'ugel_users';

    public $id;
    public $name;
    public $paternal_surname;
    public $maternal_surname;
    public $dni;
    public $group_id;
    public $subgroup_id;
    public $position;
    public $files_charge;
    public $birthdate;
    public $age;
    public $phone;
    public $email;
    public $address;
    public $active;
    public $deleted_at;
    public $user_id;

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

    public function save()
    {
        $db = Database::getConnection();
        if ($this->id) {
            $query = "UPDATE ugel_users SET 
                        name = :name, paternal_surname = :paternal_surname, maternal_surname = :maternal_surname, dni = :dni, group_id = :group_id, subgroup_id = :subgroup_id, 
                        position = :position, files_charge = :files_charge, birthdate = :birthdate, age = :age, 
                        phone = :phone, email = :email, address = :address, active = :active, deleted_at = :deleted_at, user_id = :user_id 
                      WHERE id = :id";

            $stmt = $db->prepare($query);
            $stmt->execute([
                ':id' => $this->id,
                ':name' => $this->name,
                ':paternal_surname' => $this->paternal_surname,
                ':maternal_surname' => $this->maternal_surname,
                ':dni' => $this->dni,
                ':group_id' => $this->group_id,
                ':subgroup_id' => $this->subgroup_id,
                ':position' => $this->position,
                ':files_charge' => $this->files_charge,
                ':birthdate' => $this->birthdate,
                ':age' => $this->age,
                ':phone' => $this->phone,
                ':email' => $this->email,
                ':address' => $this->address,
                ':active' => $this->active,
                ':deleted_at' => $this->deleted_at,
                ':user_id' => $this->user_id
            ]);
        } else {
            $query = "INSERT INTO ugel_users 
                        (name, paternal_surname, maternal_surname, dni, group_id, subgroup_id, position, files_charge, birthdate, age, phone, email, address, active, deleted_at, user_id) 
                      VALUES 
                        (:name, :paternal_surname, :maternal_surname, :dni, :group_id, :subgroup_id, :position, :files_charge, :birthdate, :age, :phone, :email, :address, :active, :deleted_at, :user_id)";

            $stmt = $db->prepare($query);
            $stmt->execute([
                ':name' => $this->name,
                ':paternal_surname' => $this->paternal_surname,
                ':maternal_surname' => $this->maternal_surname,
                ':dni' => $this->dni,
                ':group_id' => $this->group_id,
                ':subgroup_id' => $this->subgroup_id,
                ':position' => $this->position,
                ':files_charge' => $this->files_charge,
                ':birthdate' => $this->birthdate,
                ':age' => $this->age,
                ':phone' => $this->phone,
                ':email' => $this->email,
                ':address' => $this->address,
                ':active' => $this->active,
                ':deleted_at' => $this->deleted_at,
                ':user_id' => $this->user_id
            ]);

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

    public function user()
    {
        return User::find($this->user_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;
    }
}
