<?php

namespace App\Repositories;

use PDO;
use App\Config\DatasourceConfig;
use App\Helpers\SlaMapper;

final class SlaRepo
{
  private PDO $connection;
  private SlaMapper $mapper;

  public function __construct()
  {
    $this->connection = (new DatasourceConfig())->connect();
    $this->mapper = new SlaMapper();
  }

  private string $querySelect = "
        SELECT
            id_sla,
            nama_sla,
            waktu_respon_jam,
            waktu_penyelesaian_jam,
            deskripsi,
            created_at,
            updated_at
        FROM sla
    ";

  public function findAll()
  {
    $statement = $this->connection->prepare($this->querySelect);
    $statement->execute();

    $rows = $statement->fetchAll(PDO::FETCH_ASSOC);

    return array_map([$this->mapper, 'toSla'], $rows);
  }

  public function findById(int $id)
  {
    $statement = $this->connection->prepare(
      $this->querySelect . " WHERE id_sla = :id"
    );

    $statement->execute([
      'id' => $id
    ]);

    $row = $statement->fetch(PDO::FETCH_ASSOC);

    if (empty($row)) {
      return null;
    }

    return $this->mapper->toSla($row);
  }

  public function save(
    string $namaSla,
    int $waktuResponJam,
    int $waktuPenyelesaianJam,
    ?string $deskripsi = null
  ) {
    $query = "
            INSERT INTO sla (
                nama_sla,
                waktu_respon_jam,
                waktu_penyelesaian_jam,
                deskripsi
            )
            VALUES (
                :namaSla,
                :waktuResponJam,
                :waktuPenyelesaianJam,
                :deskripsi
            );
        ";

    $statement = $this->connection->prepare($query);

    $statement->execute([
      'namaSla' => $namaSla,
      'waktuResponJam' => $waktuResponJam,
      'waktuPenyelesaianJam' => $waktuPenyelesaianJam,
      'deskripsi' => $deskripsi
    ]);
  }

  public function update(
    int $id,
    string $namaSla,
    int $waktuResponJam,
    int $waktuPenyelesaianJam,
    ?string $deskripsi = null
  ) {
    $query = "
            UPDATE sla
            SET
                nama_sla = :namaSla,
                waktu_respon_jam = :waktuResponJam,
                waktu_penyelesaian_jam = :waktuPenyelesaianJam,
                deskripsi = :deskripsi
            WHERE id_sla = :id;
        ";

    $statement = $this->connection->prepare($query);

    $statement->execute([
      'id' => $id,
      'namaSla' => $namaSla,
      'waktuResponJam' => $waktuResponJam,
      'waktuPenyelesaianJam' => $waktuPenyelesaianJam,
      'deskripsi' => $deskripsi
    ]);
  }

  public function delete(int $id)
  {
    $statement = $this->connection->prepare(
      "DELETE FROM sla WHERE id_sla = :id"
    );

    $statement->execute([
      'id' => $id
    ]);
  }

  public function findByNamaSla(string $namaSla)
  {
    $statement = $this->connection->prepare(
      $this->querySelect . " WHERE nama_sla = :namaSla"
    );

    $statement->execute([
      'namaSla' => $namaSla
    ]);

    $rows = $statement->fetchAll(PDO::FETCH_ASSOC);

    return array_map([$this->mapper, 'toSla'], $rows);
  }
}
