<?php

namespace App\Http\Controllers\Superadmin;

use App\DTOs\SuperAdmin\ContractTemplateDto;
use App\Http\Controllers\Controller;
use App\Http\Requests\SuperAdmin\ContractTemplateRequest;
use App\Http\Requests\SuperAdmin\UpdateContractTemplateRequest;
use App\Services\ContractTemplateService;
use App\Services\SectionRuleService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class ContractTemplateController extends Controller
{
    public function __construct(protected ContractTemplateService $contractTemplateService, protected SectionRuleService $sectionRuleService)
    {

    }

    public function index(Request $request): View
    {
        $perPage = $request->get('per_page', 10);
        $contracts_templates = $this->contractTemplateService->getPaginated($perPage);
        return view('superadmin.contract-template.index', compact('contracts_templates'));
    }

    public function create(): View
    {
        $controlRules = $this->sectionRuleService->getAll();
        return view('superadmin.contract-template.create', compact('controlRules'));
    }

    public function store(ContractTemplateRequest $request): RedirectResponse
    {
        $dto = new ContractTemplateDto($request->validated());
        $created = $this->contractTemplateService->create($dto);

        if ($created) {
            return redirect()
                ->route('superadmin.contract-template.index')
                ->with('success', 'Plantilla de contracto creada correctamente.');
        }

        return back()->with('error', 'No se pudo crear la plantilla de contracto.')->withInput();
    }

    public function show(int $id)
    {
        $dto = $this->contractTemplateService->find($id);
        if (!$dto) {
            abort(404, 'Plantilla de contracto no encontrada.');
        }

        return view('superadmin.contract-template.show', compact('dto', 'id'));
    }

    public function edit(int $id): View
    {
        $dto = $this->contractTemplateService->find($id);
        if (!$dto) {
            abort(404);
        }
        $controlRules = $this->sectionRuleService->getAll();
        return view('superadmin.contract-template.edit', compact('dto', 'id', 'controlRules'));
    }

    public function update(UpdateContractTemplateRequest $request, int $id): RedirectResponse
    {
        $dto = new ContractTemplateDto($request->validated());
        $updated = $this->contractTemplateService->update($id, $dto);

        if ($updated) {
            return redirect()
                ->route('superadmin.contract-template.index')
                ->with('success', 'Plantilla de contrato actualizada correctamente.');
        }

        return back()->with('error', 'No se pudo actualizar la plantilla de contrato.')->withInput();
    }

    public function destroy(int $id): RedirectResponse
    {
        $deleted = $this->contractTemplateService->delete($id);

        if ($deleted) {
            return redirect()
                ->route('superadmin.contract-template.index')
                ->with('success', 'Plantilla de contracto eliminada correctamente.');
        }

        return back()->with('error', 'No se pudo eliminar la plantilla de contrato.');
    }
}
