<?php

namespace App\Models;

use PDO;
use Config\Database;

class UserComunity extends BaseModel
{
    protected static $table = 'comunity_users';

    public $id;
    public $name;
    public $paternal_surname;
    public $maternal_surname;
    public $dni;
    public $company_name;
    public $rucglobal;
    public $ruc;
    public $phone;
    public $email;
    public $address;
    public $user_id;
    public $active;
    public $deleted_at;

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

    public function save()
    {
        $db = Database::getConnection();
        if ($this->id) {
            $query = "UPDATE comunity_users SET 
                        name = :name, paternal_surname = :paternal_surname, maternal_surname = :maternal_surname, dni = :dni, 
                        company_name = :company_name, rucglobal = :rucglobal, ruc = :ruc, 
                        phone = :phone, email = :email, address = :address, user_id = :user_id, 
                        active = :active, deleted_at = :deleted_at 
                      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,
                ':company_name' => $this->company_name,
                ':rucglobal' => $this->rucglobal,
                ':ruc' => $this->ruc,
                ':phone' => $this->phone,
                ':email' => $this->email,
                ':address' => $this->address,
                ':user_id' => $this->user_id,
                ':active' => $this->active,
                ':deleted_at' => $this->deleted_at
            ]);

            return $this->id;

        } else {
            $query = "INSERT INTO comunity_users 
                        (name, paternal_surname, maternal_surname, dni, company_name, rucglobal, ruc, phone, email, address, user_id, active, deleted_at) 
                      VALUES 
                        (:name, :paternal_surname, :maternal_surname, :dni, :company_name, :rucglobal, :ruc, :phone, :email, :address, :user_id, :active, :deleted_at)";

            $stmt = $db->prepare($query);
            $stmt->execute([
                ':name' => $this->name,
                ':paternal_surname' => $this->paternal_surname,
                ':maternal_surname' => $this->maternal_surname,
                ':dni' => $this->dni,
                ':company_name' => $this->company_name,
                ':rucglobal' => $this->rucglobal,
                ':ruc' => $this->ruc,
                ':phone' => $this->phone,
                ':email' => $this->email,
                ':address' => $this->address,
                ':user_id' => $this->user_id,
                ':active' => $this->active,
                ':deleted_at' => $this->deleted_at
            ]);

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

    public function user()
    {
        return User::find($this->user_id);
    }

    public function personTypeIds()
    {
        $userPersonTypes = UserPersonType::where('comunity_user_id', $this->id);
        return array_map(fn($upt) => $upt->person_type_id, $userPersonTypes);
    }

    public function personTypeDescriptions()
    {
        $userPersonTypes = UserPersonType::where('comunity_user_id', $this->id);

        $descriptions = [];

        foreach ($userPersonTypes as $upt) {
            $type = PersonType::find($upt->person_type_id);
            if ($type) {
                $descriptions[] = strtoupper(trim($type->description));
            }
        }

        return $descriptions;
    }

}
