<?php

namespace api\classes;

use api\classes\API;
use JetBrains\PhpStorm\NoReturn;
use PDO;
use api\classes\API_companies;

require_once 'API.php';
require_once 'API_companies.php';

class API_office_travel_reimburse extends API
{
    #[NoReturn]
    public function createTravelReimburse(): void
    {
        $query = " INSERT INTO office_travelreimburse (
                        reimburse_uuid, 
                        user_uuid, 
                        departure_postcode, 
                        destination_postcode, 
                        travel_distance, 
                        travel_description, 
                        travel_date, 
                        office_travelreimburse_company_uuid, 
                        office_travelreimburse_company_name,
                        homework
                    )
                    VALUES (UUID(), 
                        :user_uuid, 
                        :departure_postcode, 
                        :destination_postcode, 
                        :travel_distance, 
                        :travel_description, 
                        :travel_date, 
                        :office_travelreimburse_company_uuid,
                        :office_travelreimburse_company_name,
                        :homework
                    )";

        $stmt = $this->pdo->prepare($query);

        $stmt->bindValue(':user_uuid', $this->data['user_uuid']);
        $stmt->bindValue(':departure_postcode', $this->data['departure_postcode']);
        $stmt->bindValue(':destination_postcode', $this->data['destination_postcode']);
        $stmt->bindValue(':travel_distance', $this->data['travel_distance']);
        $stmt->bindValue(':travel_date', $this->data['travel_date']);
        $stmt->bindValue(':office_travelreimburse_company_uuid', $this->data['office_travelreimburse_company_uuid']);
        $stmt->bindValue(':office_travelreimburse_company_name', $this->data['office_travelreimburse_company_name']);
        $stmt->bindValue(':homework', $this->data['homework']);

        // Optional fields
        $stmt->bindValue(':travel_description', $this->data['travel_description'] ?? null, $this->data['travel_description'] ?? null ? PDO::PARAM_STR : PDO::PARAM_NULL);

        $stmt->execute();

        $this->apiOutput(200, ['success' => 'Travel reimburse added.']);
    }

    public function getTravelReimburse(): array
    {
        list($query, $types, $params) = $this->buildDynamicQuery('office_travelreimburse');

        $items = $this->generalGetFunction($query, $types, $params, false, 'TravelReimburse');

        return ($items);
    }

    #[NoReturn]
    public function updateTravelReimburse(): void
    {
        $query = "UPDATE office_travelreimburse SET
            user_uuid = :user_uuid,
            departure_postcode = :departure_postcode,
            destination_postcode = :destination_postcode,
            travel_date = :travel_date,
            travel_distance = :travel_distance,
            travel_description = :travel_description,
            office_travelreimburse_company_uuid = :office_travelreimburse_company_uuid,
            office_travelreimburse_company_name = :office_travelreimburse_company_name,
            homework = :homework
        WHERE reimburse_uuid = :reimburse_uuid";

        $stmt = $this->pdo->prepare($query);

        $stmt->bindValue(':reimburse_uuid', $this->data['reimburse_uuid'], PDO::PARAM_STR);
        $stmt->bindValue(':user_uuid', $this->data['user_uuid'], PDO::PARAM_STR);
        $stmt->bindValue(':departure_postcode', $this->data['departure_postcode'], PDO::PARAM_STR);
        $stmt->bindValue(':destination_postcode', $this->data['destination_postcode'], PDO::PARAM_STR);
        $stmt->bindValue(':travel_date', $this->data['travel_date'], PDO::PARAM_STR);
        $stmt->bindValue(':travel_distance', $this->data['travel_distance'], PDO::PARAM_INT);
        $stmt->bindValue(':office_travelreimburse_company_uuid', $this->data['office_travelreimburse_company_uuid'], PDO::PARAM_STR);
        $stmt->bindValue(':office_travelreimburse_company_name', $this->data['office_travelreimburse_company_name'], PDO::PARAM_STR);
        $stmt->bindValue(':homework', $this->data['homework'], PDO::PARAM_BOOL);

        // Optional fields
        $stmt->bindValue(':travel_description', $this->data['travel_description'] ?? null, isset($this->data['travel_description']) && $this->data['travel_description'] !== null ? PDO::PARAM_STR : PDO::PARAM_NULL);

        $stmt->execute();

        if ($stmt->rowCount() === 0) {
            $check = $this->pdo->prepare(
                "SELECT 1 FROM office_travelreimburse WHERE reimburse_uuid = :uuid"
            );
            $check->execute([
                ':uuid' => $this->data['reimburse_uuid']
            ]);

            if (!$check->fetchColumn()) {
                $this->apiOutput(404, ['error' => 'Travel reimbursement not found.']);
            }
        }

        $this->apiOutput(200, ['success' => 'Travel reimbursement updated.']);
    }

    #[NoReturn]
    public function deleteTravelReimburse(): void
    {
        $query = "DELETE FROM office_travelreimburse WHERE reimburse_uuid = ? AND user_uuid = ?";
        $stmt = $this->prepareStatement($query);
        $stmt->bind_param('ss', $this->data['reimburse_uuid'], $_SESSION['user']['user_uuid']);
        $this->executeStatement($stmt);

        if ($stmt->affected_rows === 0) {
            $stmt->close();
            $this->apiOutput(403, ['error' => 'Travel reimbursement not found or you do not have permission to delete it.']);
        }

        $stmt->close();

        $this->apiOutput(200, ['success' => 'Travel reimburse removed.']);
    }

    public function setTravelReimburseCompany(): void
    {
        $API_companies = new API_companies();
        if (!empty($GLOBALS['modules_enabled']['customers'])) {

            $companies = $API_companies->getCompanies();

            $companyUuid = $this->postedData['office_travelreimburse_company_uuid'] ?? null;
            if ($companyUuid) {
                foreach ($companies as $company) {
                    if ($company['company_uuid'] === $companyUuid) {
                        $this->postedData['office_travelreimburse_company_name'] = $company['company_name'];
                        break;
                    }
                }
            }
        }
    }
}