<?php

namespace App\Repositories;

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

final class StatusTicketRepo
{
  private PDO $connection;
  private StatusTicketMapper $mapper;

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

  private string $querySelect = "
        SELECT
            id_status,
            nama_status,
            urutan,
            deskripsi
        FROM status_ticket
    ";

  public function findAll()
  {
    $statement = $this->connection->prepare(
      $this->querySelect . " ORDER BY urutan ASC"
    );

    $statement->execute();

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

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

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

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

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

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

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

  public function save(
    string $namaStatus,
    int $urutan,
    ?string $deskripsi = null
  ) {
    $query = "
            INSERT INTO status_ticket (
                nama_status,
                urutan,
                deskripsi
            )
            VALUES (
                :namaStatus,
                :urutan,
                :deskripsi
            );
        ";

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

    $statement->execute([
      'namaStatus' => $namaStatus,
      'urutan' => $urutan,
      'deskripsi' => $deskripsi
    ]);
  }

  public function update(
    int $id,
    string $namaStatus,
    int $urutan,
    ?string $deskripsi = null
  ) {
    $query = "
            UPDATE status_ticket
            SET
                nama_status = :namaStatus,
                urutan = :urutan,
                deskripsi = :deskripsi
            WHERE id_status = :id;
        ";

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

    $statement->execute([
      'id' => $id,
      'namaStatus' => $namaStatus,
      'urutan' => $urutan,
      'deskripsi' => $deskripsi
    ]);
  }

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

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

  public function findByNamaStatus(string $namaStatus)
  {
    $statement = $this->connection->prepare(
      $this->querySelect . " WHERE nama_status = :namaStatus"
    );

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

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

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


  public function findByNames(array $names): array
  {
    if (empty($names)) {
      return [];
    }

    $placeholders = implode(',', array_fill(0, count($names), '?'));

    $query = "
        SELECT
            id_status,
            nama_status
        FROM status_ticket
        WHERE nama_status IN ($placeholders)
        ORDER BY urutan
    ";

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

    return $statement->fetchAll(PDO::FETCH_ASSOC);
  }
}
