<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\CertificateTemplate;
use App\Models\Participant;
use App\Services\ActivityLogService;
use App\Services\CertificateGeneratorService;
use App\Services\CertificateTemplateService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\View\View;
use Symfony\Component\HttpFoundation\Response;

class CertificateTemplateController extends Controller
{
    public function __construct(
        private readonly CertificateTemplateService $templateService,
        private readonly CertificateGeneratorService $certificateGenerator,
        private readonly ActivityLogService $activityLogService,
    ) {}

    public function index(): View
    {
        $templates = CertificateTemplate::orderByDesc('created_at')->paginate(10);

        return view('admin.certificate-templates.index', compact('templates'));
    }

    public function create(): View
    {
        return view('admin.certificate-templates.create');
    }

    public function store(Request $request): RedirectResponse
    {
        $data = $request->validate([
            'template_name' => 'required|string|max:255',
            'template_file' => 'required|file|mimes:png,jpg,jpeg,pdf|max:20480',
            'page_size' => 'required|string',
            'orientation' => 'required|in:portrait,landscape',
            'name_position_x' => 'required|integer|min:0',
            'name_position_y' => 'required|integer|min:0',
            'name_width' => 'required|integer|min:100',
            'name_font' => 'required|string',
            'name_font_size' => 'required|integer|min:12|max:72',
            'name_font_color' => 'required|string',
            'name_alignment' => 'required|in:left,center,right',
            'name_text_transform' => 'required|in:none,uppercase,lowercase,capitalize',
            'is_active' => 'boolean',
        ]);

        $template = $this->templateService->store($request->file('template_file'), $data);
        $this->activityLogService->log('upload', 'certificate_templates', 'Muat naik template: '.$template->template_name, $template);

        return redirect()->route('admin.certificate-templates.index')->with('success', 'Template sijil berjaya dimuat naik.');
    }

    public function show(CertificateTemplate $certificateTemplate): View
    {
        return view('admin.certificate-templates.edit', ['template' => $certificateTemplate]);
    }

    public function edit(CertificateTemplate $certificateTemplate): View
    {
        return view('admin.certificate-templates.edit', ['template' => $certificateTemplate]);
    }

    public function update(Request $request, CertificateTemplate $certificateTemplate): RedirectResponse
    {
        $data = $request->validate([
            'template_name' => 'required|string|max:255',
            'template_file' => 'nullable|file|mimes:png,jpg,jpeg,pdf|max:20480',
            'page_size' => 'required|string',
            'orientation' => 'required|in:portrait,landscape',
            'name_position_x' => 'required|integer|min:0',
            'name_position_y' => 'required|integer|min:0',
            'name_width' => 'required|integer|min:100',
            'name_font' => 'required|string',
            'name_font_size' => 'required|integer|min:12|max:72',
            'name_font_color' => 'required|string',
            'name_alignment' => 'required|in:left,center,right',
            'name_text_transform' => 'required|in:none,uppercase,lowercase,capitalize',
            'is_active' => 'boolean',
        ]);

        $this->templateService->update($certificateTemplate, $data, $request->file('template_file'));
        $this->activityLogService->log('update', 'certificate_templates', 'Kemas kini template: '.$certificateTemplate->template_name, $certificateTemplate);

        return redirect()->route('admin.certificate-templates.index')->with('success', 'Template sijil berjaya dikemas kini.');
    }

    public function destroy(CertificateTemplate $certificateTemplate): RedirectResponse
    {
        if ($certificateTemplate->template_file && Storage::disk('local')->exists($certificateTemplate->template_file)) {
            Storage::disk('local')->delete($certificateTemplate->template_file);
        }

        $certificateTemplate->delete();
        $this->activityLogService->log('delete', 'certificate_templates', 'Padam template: '.$certificateTemplate->template_name);

        return back()->with('success', 'Template sijil berjaya dipadam.');
    }

    public function preview(CertificateTemplate $certificateTemplate): Response
    {
        $sampleParticipant = new Participant([
            'full_name' => 'MUHAMMAD NUR HAKIM BIN ABDUL RAHMAN',
            'id' => 0,
        ]);

        return $this->certificateGenerator->download($sampleParticipant, $certificateTemplate);
    }

    public function activate(CertificateTemplate $certificateTemplate): RedirectResponse
    {
        $this->templateService->activate($certificateTemplate);

        return back()->with('success', 'Template sijil telah diaktifkan.');
    }
}
