<?php

declare(strict_types=1);

namespace App\Repository;

final class TestimonioRepository
{
    private \PDO $database;

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

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

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

        return $Testimonio;
    }

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

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

    public function create(object $Testimonio): object
    {
        $query = 'INSERT INTO `Testimonio` (`NombreCompleto`, `CorreoElectronico`, `Testimonio`, `Condiciones`, `field1`, `field2`, `field3`, `field4`, `field5`) VALUES (:NombreCompleto, :CorreoElectronico, :Testimonio, :Condiciones, :field1, :field2, :field3, :field4, :field5)';
        $statement = $this->getDb()->prepare($query);
//         $statement->bindParam('id', $Testimonio->id);
//         $statement->bindParam('fecha', $Testimonio->fecha);
        $statement->bindParam('NombreCompleto', $Testimonio->NombreCompleto);
        $statement->bindParam('CorreoElectronico', $Testimonio->CorreoElectronico);
        $statement->bindParam('Testimonio', $Testimonio->Testimonio);
        $statement->bindParam('Condiciones', $Testimonio->Condiciones);
        $statement->bindParam('field1', $Testimonio->field1);
        $statement->bindParam('field2', $Testimonio->field2);
        $statement->bindParam('field3', $Testimonio->field3);
        $statement->bindParam('field4', $Testimonio->field4);
        $statement->bindParam('field5', $Testimonio->field5);

        $statement->execute();

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

    public function update(object $Testimonio, object $data): object
    {
        if (isset($data->fecha)) {
            $Testimonio->fecha = $data->fecha;
        }
        if (isset($data->NombreCompleto)) {
            $Testimonio->NombreCompleto = $data->NombreCompleto;
        }
        if (isset($data->CorreoElectronico)) {
            $Testimonio->CorreoElectronico = $data->CorreoElectronico;
        }
        if (isset($data->Testimonio)) {
            $Testimonio->Testimonio = $data->Testimonio;
        }
        if (isset($data->Condiciones)) {
            $Testimonio->Condiciones = $data->Condiciones;
        }
        if (isset($data->field1)) {
            $Testimonio->field1 = $data->field1;
        }
        if (isset($data->field2)) {
            $Testimonio->field2 = $data->field2;
        }
        if (isset($data->field3)) {
            $Testimonio->field3 = $data->field3;
        }
        if (isset($data->field4)) {
            $Testimonio->field4 = $data->field4;
        }
        if (isset($data->field5)) {
            $Testimonio->field5 = $data->field5;
        }

        $query = 'UPDATE `Testimonio` SET `fecha` = :fecha, `NombreCompleto` = :NombreCompleto, `CorreoElectronico` = :CorreoElectronico, `Testimonio` = :Testimonio, `Condiciones` = :Condiciones, `field1` = :field1, `field2` = :field2, `field3` = :field3, `field4` = :field4, `field5` = :field5 WHERE `id` = :id';
        $statement = $this->getDb()->prepare($query);
        $statement->bindParam('id', $Testimonio->id);
        $statement->bindParam('fecha', $Testimonio->fecha);
        $statement->bindParam('NombreCompleto', $Testimonio->NombreCompleto);
        $statement->bindParam('CorreoElectronico', $Testimonio->CorreoElectronico);
        $statement->bindParam('Testimonio', $Testimonio->Testimonio);
        $statement->bindParam('Condiciones', $Testimonio->Condiciones);
        $statement->bindParam('field1', $Testimonio->field1);
        $statement->bindParam('field2', $Testimonio->field2);
        $statement->bindParam('field3', $Testimonio->field3);
        $statement->bindParam('field4', $Testimonio->field4);
        $statement->bindParam('field5', $Testimonio->field5);

        $statement->execute();

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

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