<?php

namespace App\Models;

use PDO;
use Config\Database;

class Role extends BaseModel
{
    protected static $table = 'roles';

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

}
