<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use App\Models\Invoices;
use App\Services\PoliceServices\RenewServices;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class PolicyRenewController extends Controller
{
    protected $renewServices;

    public function __construct()
    {
        $this->renewServices = new RenewServices();
    }
    public function UpdatePolice(Request $request)
    {
        $newServicesString = json_encode($request->newServices);

        // Usar la fecha seleccionada por el usuario si existe, si no usar la lógica anterior
        if ($request->has('newPolicyStartDate') && !empty($request->newPolicyStartDate)) {
            $newDate = $request->newPolicyStartDate;
        } else {
            // Lógica corregida para evitar el error de "un día menos"
            // Extraer solo la fecha YYYY-MM-DD para ignorar la hora/zona (posiblemente incorrecta) del dato original
            $dateOnly = substr($request->invoice['policyInitDate'], 0, 10);
            
            try {
                // Parsear fecha en la zona horaria de la app
                $date = Carbon::createFromFormat('Y-m-d', $dateOnly, config('app.timezone'))->startOfDay();
            } catch (\Exception $e) {
                // Fallback seguro
                $date = Carbon::parse($request->invoice['policyInitDate']);
            }

            $date->addMonths(6);

            // Validar si es fecha pasada
            if ($date->isPast()) {
                $date = Carbon::now();
            }

            // Convertir a UTC real para el formato ISO 8601 Z
            $newDate = $date->utc()->format('Y-m-d\TH:i:s.u\Z');
        }

        $invoice = new Invoices();
        $invoice->chassis = strtoupper($request->invoice['chassis']);
        $invoice->licensePlate = strtoupper($request->invoice['licensePlate']);
        $invoice->year = $request->invoice['year'];
        $invoice->sellers_id = $request->invoice['sellers_id'];
        $invoice->car_brand = $request->invoice['car_brand'];
        $invoice->car_model = $request->invoice['car_model'];
        $invoice->car_tipe = $request->invoice['car_tipe'];
        $invoice->client_id = $request->invoice['client_id'];
        $invoice->payment_status = 'peding';
        $invoice->policyInitDate = $newDate;
        $invoice->services = $newServicesString;
        $invoice->policyTime = $request->time;
        $invoice->totalGeneral = $request->policePrice;
        $invoice->discount_id = $request->discount_id;
        $invoice->save();

        return $invoice;
    }

    public function sendMessengerRenew()
    {
        set_time_limit(0);
        return $this->renewServices->sendMessengerRenew();
    }
}
