<?php

namespace App\Models;

use PDO;
use Config\Database;

class Response extends BaseModel
{
    protected static $table = 'responses';

    public $id;
    public $process_id;
    public $user_id;
    public $resolution_number;
    public $comment;
    public $response_type_id;
    public $active;
    public $created_at;
    public $deleted_at;
    public $is_read;
    public $read_at;
    public $file_name;
    public $file_path;
    public $downloaded_at;

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

    public function save()
    {
        $db = Database::getConnection();
        
        if ($this->id) {
            $query = "UPDATE responses 
                    SET process_id = :process_id, user_id = :user_id, 
                        resolution_number = :resolution_number, comment = :comment, 
                        response_type_id = :response_type_id, active = :active, 
                        is_read = :is_read, read_at = :read_at, file_name = :file_name, file_path = :file_path, downloaded_at = :downloaded_at
                    WHERE id = :id";
            $stmt = $db->prepare($query);
            $stmt->execute([
                ':id' => $this->id,
                ':process_id' => $this->process_id,
                ':user_id' => $this->user_id,
                ':resolution_number' => $this->resolution_number,
                ':comment' => $this->comment,
                ':response_type_id' => $this->response_type_id,
                ':active' => $this->active,
                ':is_read' => $this->is_read,
                ':read_at' => $this->read_at,
                ':file_name' => $this->file_name,
                ':file_path' => $this->file_path,
                ':downloaded_at' => $this->downloaded_at
            ]);
        } else {
            $query = "INSERT INTO responses 
                    (process_id, user_id, resolution_number, comment, 
                    response_type_id, active, created_at, is_read, read_at, file_name, file_path, downloaded_at) 
                    VALUES (:process_id, :user_id, :resolution_number, :comment, 
                            :response_type_id, :active, NOW(), :is_read, :read_at, :file_name, :file_path, :downloaded_at)";
            $stmt = $db->prepare($query);
            $stmt->execute([
                ':process_id' => $this->process_id,
                ':user_id' => $this->user_id,
                ':resolution_number' => $this->resolution_number,
                ':comment' => $this->comment,
                ':response_type_id' => $this->response_type_id,
                ':active' => $this->active,
                ':is_read' => $this->is_read ?? 0,
                ':read_at' => $this->read_at,
                ':file_name' => $this->file_name,
                ':file_path' => $this->file_path,
                ':downloaded_at' => $this->downloaded_at
            ]);
            $this->id = $db->lastInsertId();
        }
    }


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

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

    public function responseType()
    {
        return ResponseType::find($this->response_type_id);
    }

    public function isProveido()
    {
        return strtolower($this->responseType()->description) === 'proveido';
    }

    public function isResolution()
    {
        return strtolower($this->responseType()->description) === 'resolucion';
    }
    
}