<?php

namespace App\Repositories;

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

final class TicketAssignmentRepo
{

  private PDO $connection;
  private TicketAssignmentMapper $mapper;

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

  public function save(
    ?PDO $connection,
    int $ticketId,
    int $teknisiId,
    int $statusId,
    ?string $catatan
  ): int {

    $query = "
        INSERT INTO ticket_assignments(
            id_ticket,
            id_teknisi,
            id_status,
            catatan
        )
        VALUES(
            :ticketId,
            :teknisiId,
            :statusId,
            :catatan
        )
    ";

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

    $statement->execute([
      'ticketId' => $ticketId,
      'teknisiId' => $teknisiId,
      'statusId' => $statusId,
      'catatan' => $catatan
    ]);

    return (int) $connection->lastInsertId();
  }

  public function existsActiveAssignment(
    int $ticketId
  ): bool {

    $query = "
        SELECT COUNT(*)
        FROM ticket_assignments
        WHERE id_ticket = :ticketId
        AND status_penugasan IN ('DITUGASKAN', 'DIPROSES')
    ";

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

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

    return (bool) $statement->fetchColumn();
  }

  // update
  public function update(
    string $teknisiId,
    string $statusPenugasan,
    string $catatan,
    int $ticketId,
    ?bool $solved
  ): void {

    $query = "
        UPDATE ticket_assignments
        SET id_teknisi = :teknisiId,
        status_penugasan = :statusPenugasan,
        catatan = :catatan,
        tanggal_selesai = :solved
        WHERE id_ticket = :ticketId
    ";

    $statement = $this->connection->prepare($query);
    $statement->execute([
      'teknisiId' => $teknisiId,
      'statusPenugasan' => $statusPenugasan,
      'catatan' => $catatan,
      'ticketId' => $ticketId,
      'solved' => $solved ? date('Y-m-d H:i:s') : null
    ]);
  }

  // find by ticket id
  public function findByTicketId(int $ticketId)
  {
    $query = "
        SELECT * FROM ticket_assignments WHERE id_ticket = :ticketId
    ";

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

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


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

    if (!$row) {
      return null;
    }

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

  // find by teknisi id
  public function findByTeknisiId(int $teknisiId)
  {
    $query = "
        SELECT * FROM ticket_assignments WHERE id_teknisi = :teknisiId
    ";

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

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


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

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