<?php

namespace App\Filament\Resources\Contracts\Pages;

use App\Filament\Resources\Contracts\ContractResource;
use App\Services\ContractScheduleService;
use Filament\Resources\Pages\CreateRecord;

class CreateContract extends CreateRecord
{
    protected static string $resource = ContractResource::class;

    protected function afterCreate(): void
    {
        $contract = $this->record;

        $template = null;

        if ($contract->payment_template_id) {
            $template = $contract->paymentTemplate;
        } elseif ($contract->sales_phase_id && $contract->salesPhase?->payment_template_id) {
            $template = $contract->salesPhase->paymentTemplate;
        }

        if ($template) {
            ContractScheduleService::generateFromTemplate($contract, $template);
        }
    }
}
