<?php

declare(strict_types=1);

namespace App\Repository;

final class AccionRepository
{
    private \PDO $database;

    public function __construct(\PDO $database)
    {
        $this->database = $database;
    }

    public function getDb(): \PDO
    {
        return $this->database;
    }

    public function checkAndGet(int $AccionId): object
    {
        $query = 'SELECT * FROM `Accion` WHERE `id` = :id';
        $statement = $this->getDb()->prepare($query);
        $statement->bindParam('id', $AccionId);
        $statement->execute();
        $Accion = $statement->fetchObject();
        if (! $Accion) {
            throw new \Exception('Accion not found.', 404);
        }

        return $Accion;
    }

    public function getAll(): array
    {
        $query = 'SELECT * FROM `Accion` ORDER BY `id`';
        $statement = $this->getDb()->prepare($query);
        $statement->execute();

        return (array) $statement->fetchAll();
    }

    public function create(object $Accion): object
    {
        $query = 'INSERT INTO `Accion` (`perfil`, `modulo`, `_save`, `_edit`, `_update`, `_delete`, `_view`, `field1`, `field2`, `field3`, `field4`, `field5`) VALUES (:perfil, :modulo, :_save, :_edit, :_update, :_delete, :_view, :field1, :field2, :field3, :field4, :field5)';
        $statement = $this->getDb()->prepare($query);
//         $statement->bindParam('id', $Accion->id);
//         $statement->bindParam('fecha', $Accion->fecha);
        $statement->bindParam('perfil', $Accion->perfil);
        $statement->bindParam('modulo', $Accion->modulo);
        $statement->bindParam('_save', $Accion->_save);
        $statement->bindParam('_edit', $Accion->_edit);
        $statement->bindParam('_update', $Accion->_update);
        $statement->bindParam('_delete', $Accion->_delete);
        $statement->bindParam('_view', $Accion->_view);
        $statement->bindParam('field1', $Accion->field1);
        $statement->bindParam('field2', $Accion->field2);
        $statement->bindParam('field3', $Accion->field3);
        $statement->bindParam('field4', $Accion->field4);
        $statement->bindParam('field5', $Accion->field5);

        $statement->execute();

        return $this->checkAndGet((int) $this->getDb()->lastInsertId());
    }

    public function update(object $Accion, object $data): object
    {
        if (isset($data->fecha)) {
            $Accion->fecha = $data->fecha;
        }
        if (isset($data->perfil)) {
            $Accion->perfil = $data->perfil;
        }
        if (isset($data->modulo)) {
            $Accion->modulo = $data->modulo;
        }
        if (isset($data->_save)) {
            $Accion->_save = $data->_save;
        }
        if (isset($data->_edit)) {
            $Accion->_edit = $data->_edit;
        }
        if (isset($data->_update)) {
            $Accion->_update = $data->_update;
        }
        if (isset($data->_delete)) {
            $Accion->_delete = $data->_delete;
        }
        if (isset($data->_view)) {
            $Accion->_view = $data->_view;
        }
        if (isset($data->field1)) {
            $Accion->field1 = $data->field1;
        }
        if (isset($data->field2)) {
            $Accion->field2 = $data->field2;
        }
        if (isset($data->field3)) {
            $Accion->field3 = $data->field3;
        }
        if (isset($data->field4)) {
            $Accion->field4 = $data->field4;
        }
        if (isset($data->field5)) {
            $Accion->field5 = $data->field5;
        }

        $query = 'UPDATE `Accion` SET `fecha` = :fecha, `perfil` = :perfil, `modulo` = :modulo, `_save` = :_save, `_edit` = :_edit, `_update` = :_update, `_delete` = :_delete, `_view` = :_view, `field1` = :field1, `field2` = :field2, `field3` = :field3, `field4` = :field4, `field5` = :field5 WHERE `id` = :id';
        $statement = $this->getDb()->prepare($query);
        $statement->bindParam('id', $Accion->id);
        $statement->bindParam('fecha', $Accion->fecha);
        $statement->bindParam('perfil', $Accion->perfil);
        $statement->bindParam('modulo', $Accion->modulo);
        $statement->bindParam('_save', $Accion->_save);
        $statement->bindParam('_edit', $Accion->_edit);
        $statement->bindParam('_update', $Accion->_update);
        $statement->bindParam('_delete', $Accion->_delete);
        $statement->bindParam('_view', $Accion->_view);
        $statement->bindParam('field1', $Accion->field1);
        $statement->bindParam('field2', $Accion->field2);
        $statement->bindParam('field3', $Accion->field3);
        $statement->bindParam('field4', $Accion->field4);
        $statement->bindParam('field5', $Accion->field5);

        $statement->execute();

        return $this->checkAndGet((int) $Accion->id);
    }

    public function delete(int $AccionId): void
    {
        $query = 'DELETE FROM `Accion` WHERE `id` = :id';
        $statement = $this->getDb()->prepare($query);
        $statement->bindParam('id', $AccionId);
        $statement->execute();
    }
}
