<?php

namespace App\Controllers\Contracts;

use App\Controllers\BaseController;
use App\Models\Contracts\ContractInfoModel;
use CodeIgniter\HTTP\Response;
use CodeIgniter\API\ResponseTrait;


$allowedOrigins = [
    // 'https://catchef.bravecatstudios.com',  // Producción
    'http://localhost:4200', // Desarrollo en local
    'http://localhost:8000', // Desarrollo en local Mac Cambios que hicimos
];

if (isset($_SERVER['HTTP_ORIGIN']) && in_array($_SERVER['HTTP_ORIGIN'], $allowedOrigins)) {
    header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
}

header('Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE');
header('Access-Control-Allow-Headers: Content-Type, Authorization');
header('Access-Control-Allow-Credentials: true');

class ContractInfoController extends BaseController
{
    protected $contractModel;

    public function __construct(){
        $this->contractModel = new ContractInfoModel();
    }

    public function GetAllContracts() {
        $contracts = $this->contractModel->orderBy('PK_Contract')->findAll();

        $grouped = [];

        foreach ($contracts as $contract) {
            $id = $contract['PK_Contract'];

            // Construye la ruta completa (ajústala si tu dominio o carpeta es diferente)
            $fullPath = base_url($contract['ImagePath']);

            if (!isset($grouped[$id])) {
                $grouped[$id] = $contract;
                $grouped[$id]['ImagePaths'] = [$fullPath];
                unset($grouped[$id]['ImagePath']);
            } else {
                $grouped[$id]['ImagePaths'][] = $fullPath;
            }
        }

        return $this->response->setJSON(array_values($grouped));
    }

    public function GetContractByFk($pk){
        $contracts = $this->contractModel->where('PK_Contract', $pk)->orderBy('PK_Contract')->findAll();

        $grouped = [];

        foreach ($contracts as $contract) {
            $id = $contract['PK_Contract'];

            // Construye la ruta completa (ajústala si tu dominio o carpeta es diferente)
            $fullPath = base_url($contract['ImagePath']);

            if (!isset($grouped[$id])) {
                $grouped[$id] = $contract;
                $grouped[$id]['ImagePaths'] = [$fullPath];
                unset($grouped[$id]['ImagePath']);
            } else {
                $grouped[$id]['ImagePaths'][] = $fullPath;
            }
        }

        return $this->response->setJSON(array_values($grouped));
    }

        public function GetContractInfoById($userId){
        $contracts = $this->contractModel->where('FK_User', $userId)->orderBy('PK_Contract')->findAll();

        $grouped = [];

        foreach ($contracts as $contract) {
            $id = $contract['PK_Contract'];

            // Construye la ruta completa (ajústala si tu dominio o carpeta es diferente)
            $fullPath = base_url($contract['ImagePath']);
            

            if (!isset($grouped[$id])) {
                $grouped[$id] = $contract;
                $grouped[$id]['ImagePaths'] = []; // elimine $fullPath
                unset($grouped[$id]['ImagePath']);
            } else {
                $grouped[$id]['ImagePaths'][] = $fullPath;
            }
        }

        return $this->response->setJSON(array_values($grouped));
    }
}