<?php

namespace App\Repositories;

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

final class TeknisiRepo
{
	private PDO $connection;
	private TeknisiMapper $mapper;

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

	private string $querySelect = "
        SELECT
            t.id_teknisi,
            t.id_user,
            u.nama,
            u.username,
            t.spesialisasi,
            t.status_aktif,
            t.created_at,
            t.updated_at
        FROM teknisi t
        INNER JOIN users u
            ON u.id_user = t.id_user
    ";

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

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

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

	// find aktif teknisi
	public function findAktif()
	{
		$statement = $this->connection->prepare(
			$this->querySelect . " WHERE t.status_aktif = :statusAktif"
		);

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

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

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

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

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

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

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

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

	public function findByUserId(int $userId)
	{
		$statement = $this->connection->prepare(
			$this->querySelect . " WHERE t.id_user = :userId"
		);

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

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

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

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

	public function save(
		int $userId,
		?string $spesialisasi,
		string $statusAktif
	) {
		$query = "
            INSERT INTO teknisi (
                id_user,
                spesialisasi,
                status_aktif
            )
            VALUES (
                :userId,
                :spesialisasi,
                :statusAktif
            )
        ";

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

		$statement->execute([
			'userId' => $userId,
			'spesialisasi' => $spesialisasi,
			'statusAktif' => $statusAktif
		]);
	}

	public function update(
		int $id,
		?string $spesialisasi,
		string $statusAktif
	) {
		$query = "
            UPDATE teknisi
            SET
                spesialisasi = :spesialisasi,
                status_aktif = :statusAktif
            WHERE id_teknisi = :id
        ";

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

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

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

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

	public function findBySpesialisasi(string $spesialisasi)
	{
		$statement = $this->connection->prepare(
			$this->querySelect . " WHERE t.spesialisasi = :spesialisasi"
		);

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

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

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

	public function findByStatusAktif(string $statusAktif)
	{
		$statement = $this->connection->prepare(
			$this->querySelect . " WHERE t.status_aktif = :statusAktif"
		);

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

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

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