<?php

namespace App\Models;

use PDO;
use Config\Database;

class UserDirectorData extends BaseModel
{
    protected static $table = 'user_director_data';

    public $id;
    public $comunity_user_id;
    public $school_name;
    public $active;
    public $created_at;
    public $updated_at;
    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 user_director_data 
                      SET comunity_user_id = :comunity_user_id, school_name = :school_name, active = :active, updated_at = NOW()
                      WHERE id = :id";
            $stmt = $db->prepare($query);
            $stmt->execute([
                ':id' => $this->id,
                ':comunity_user_id' => $this->comunity_user_id,
                ':school_name' => $this->school_name,
                ':active' => $this->active
            ]);
        } else {
            $query = "INSERT INTO user_director_data (comunity_user_id, school_name, active) 
                      VALUES (:comunity_user_id, :school_name, :active)";
            $stmt = $db->prepare($query);
            $stmt->execute([
                ':comunity_user_id' => $this->comunity_user_id,
                ':school_name' => $this->school_name,
                ':active' => $this->active
            ]);
            $this->id = $db->lastInsertId();
        }
    }
}