<?php

namespace App\Http\Controllers;

use App\Models\Contract;
use App\Services\ContractService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ContractController extends Controller
{
    public function __construct(private ContractService $contractService) {}

    public function getContracts(Request $request): JsonResponse
    {
        $filters = $request->only(['active']);
        $contracts = $this->contractService->getContracts($filters);

        $data = $contracts->map(function (Contract $contract) {
            $arr = $contract->toArray();
            $arr['expiry_status'] = $contract->expiry_status;
            return $arr;
        });

        return response()->json($data);
    }

    public function getContractsToInvoice(Request $request): JsonResponse
    {
        $account = $request->query('account');
        $contracts = $this->contractService->getContractsToInvoice($account);
        return response()->json($contracts);
    }

    public function store(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'customer_name'          => 'required|string|max:255',
            'customer_id'            => 'required|integer',
            'contract_no'            => 'required|string|max:100',
            'contract_active'        => 'required|boolean',
            'contract_start_date'    => 'nullable|date',
            'date_signed'            => 'nullable|date',
            'date_end'               => 'nullable|date',
            'duration'               => 'nullable|integer',
            'auto_renewal'           => 'nullable|boolean',
            'payment'                => 'nullable|string|in:month,year',
            'price_per_month'        => 'nullable|numeric',
            'price_per_month_vat'    => 'nullable|numeric',
            'price_per_year'         => 'nullable|numeric',
            'price_per_year_vat'     => 'nullable|numeric',
            'primary_email'          => 'nullable|array',
            'primary_email.*'        => 'email',
            'email_cc'               => 'nullable|array',
            'email_cc.*'             => 'email',
            'email_bcc'              => 'nullable|array',
            'email_bcc.*'            => 'email',
            'eracun'                 => 'nullable|boolean',
            'send_xml'               => 'nullable|boolean',
            'send_invoice_to_email'  => 'nullable|boolean',
            'minimax_account'        => 'nullable|string|max:100',
            'minimax_customer_id'    => 'nullable|integer',
            'napomena'               => 'nullable|array',
            'contracts_archive'      => 'nullable|array',
            'description_above'      => 'nullable|string',
        ]);

        $contract = Contract::create($validated);

        return response()->json($contract, 201);
    }

    public function update(Request $request, Contract $contract): JsonResponse
    {
        $validated = $request->validate([
            'customer_name'          => 'sometimes|string|max:255',
            'customer_id'            => 'sometimes|integer',
            'contract_no'            => 'sometimes|string|max:100',
            'contract_active'        => 'sometimes|boolean',
            'contract_start_date'    => 'nullable|date',
            'date_signed'            => 'nullable|date',
            'date_end'               => 'nullable|date',
            'duration'               => 'nullable|integer',
            'auto_renewal'           => 'nullable|boolean',
            'payment'                => 'nullable|string|in:month,year',
            'price_per_month'        => 'nullable|numeric',
            'price_per_month_vat'    => 'nullable|numeric',
            'price_per_year'         => 'nullable|numeric',
            'price_per_year_vat'     => 'nullable|numeric',
            'primary_email'          => 'nullable|array',
            'primary_email.*'        => 'email',
            'email_cc'               => 'nullable|array',
            'email_cc.*'             => 'email',
            'email_bcc'              => 'nullable|array',
            'email_bcc.*'            => 'email',
            'eracun'                 => 'nullable|boolean',
            'send_xml'               => 'nullable|boolean',
            'send_invoice_to_email'  => 'nullable|boolean',
            'minimax_account'        => 'nullable|string|max:100',
            'minimax_customer_id'    => 'nullable|integer',
            'napomena'               => 'nullable|array',
            'contracts_archive'      => 'nullable|array',
            'description_above'      => 'nullable|string',
        ]);

        $contract->update($validated);

        $arr = $contract->fresh()->toArray();
        $arr['expiry_status'] = $contract->fresh()->expiry_status;

        return response()->json($arr);
    }

    public function destroy(Contract $contract): JsonResponse
    {
        $contract->delete();
        return response()->json(['message' => 'Contract deleted']);
    }
}
