<?php

namespace App\Repositories;

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

final class PriorityRepo
{
  private PDO $connection;
  private PriorityMapper $mapper;

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

  private string $querySelect = "
        SELECT
            id_prioritas,
            nama_prioritas,
            level,
            deskripsi,
            created_at,
            updated_at
        FROM priorities
    ";

  public function findAll()
  {
    $statement = $this->connection->prepare($this->querySelect);
    $statement->execute();
    $rows = $statement->fetchAll(PDO::FETCH_ASSOC);

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

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

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

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

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

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

  public function save(
    string $namaPrioritas,
    int $level,
    ?string $deskripsi = null
  ) {
    $query = "
            INSERT INTO priorities (
                nama_prioritas,
                level,
                deskripsi
            )
            VALUES (
                :namaPrioritas,
                :level,
                :deskripsi
            );
        ";

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

    $statement->execute([
      'namaPrioritas' => $namaPrioritas,
      'level' => $level,
      'deskripsi' => $deskripsi
    ]);
  }

  public function update(
    int $id,
    string $namaPrioritas,
    int $level,
    ?string $deskripsi = null
  ) {
    $query = "
            UPDATE priorities
            SET
                nama_prioritas = :namaPrioritas,
                level = :level,
                deskripsi = :deskripsi
            WHERE id_prioritas = :id;
        ";

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

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

  public function delete(int $id)
  {
    $query = "DELETE FROM priorities WHERE id_prioritas = :id;";

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

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

  // findByNamaPrioritas
  public function findByNamaPrioritas(string $namaPrioritas)
  {
    $statement = $this->connection->prepare(
      $this->querySelect . " WHERE nama_prioritas = :namaPrioritas"
    );

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

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

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

  // findByLevel
  public function findByLevel(int $level)
  {
    $statement = $this->connection->prepare(
      $this->querySelect . " WHERE level = :level"
    );

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

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

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