<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\LegalPage;
use App\Services\AdminActivityLogger;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class LegalPageController extends Controller
{
    public function index(): View
    {
        return view('admin.cms.legal', [
            'pages' => LegalPage::query()
                ->orderBy('sort_order')
                ->orderBy('id')
                ->get(),
        ]);
    }

    public function store(Request $request, AdminActivityLogger $logger): RedirectResponse
    {
        $validated = $request->validate([
            'slug' => ['required', 'string', 'max:191', 'unique:legal_pages,slug'],
            'title' => ['required', 'string', 'max:191'],
            'body' => ['nullable', 'string'],
            'meta_title' => ['nullable', 'string', 'max:191'],
            'meta_description' => ['nullable', 'string', 'max:600'],
            'canonical_url' => ['nullable', 'string', 'max:191'],
            'status' => ['nullable', 'in:draft,published'],
            'sort_order' => ['nullable', 'integer', 'min:0'],
            'is_active' => ['nullable', 'boolean'],
            'is_indexable' => ['nullable', 'boolean'],
            'og_image' => ['nullable', 'image', 'max:3072'],
        ]);

        $ogImagePath = null;
        if ($request->hasFile('og_image')) {
            $ogImagePath = $request->file('og_image')->store('legal/og', 'public');
        }

        $page = LegalPage::query()->create([
            'slug' => $validated['slug'],
            'title' => $validated['title'],
            'body' => $validated['body'] ?? null,
            'meta_title' => $validated['meta_title'] ?? null,
            'meta_description' => $validated['meta_description'] ?? null,
            'og_image_path' => $ogImagePath,
            'canonical_url' => $validated['canonical_url'] ?? null,
            'status' => $validated['status'] ?? 'published',
            'sort_order' => (int) ($validated['sort_order'] ?? 0),
            'is_active' => (bool) ($validated['is_active'] ?? true),
            'is_indexable' => (bool) ($validated['is_indexable'] ?? true),
        ]);

        $logger->log('legal_page.create', LegalPage::class, $page->id);

        return back()->with('status', 'Legal page created.');
    }

    public function update(Request $request, LegalPage $page, AdminActivityLogger $logger): RedirectResponse
    {
        $validated = $request->validate([
            'slug' => ['required', 'string', 'max:191', Rule::unique('legal_pages', 'slug')->ignore($page->id)],
            'title' => ['required', 'string', 'max:191'],
            'body' => ['nullable', 'string'],
            'meta_title' => ['nullable', 'string', 'max:191'],
            'meta_description' => ['nullable', 'string', 'max:600'],
            'canonical_url' => ['nullable', 'string', 'max:191'],
            'status' => ['nullable', 'in:draft,published'],
            'sort_order' => ['nullable', 'integer', 'min:0'],
            'is_active' => ['nullable', 'boolean'],
            'is_indexable' => ['nullable', 'boolean'],
            'og_image' => ['nullable', 'image', 'max:3072'],
        ]);

        if ($request->hasFile('og_image')) {
            $page->og_image_path = $request->file('og_image')->store('legal/og', 'public');
        }

        $page->slug = $validated['slug'];
        $page->title = $validated['title'];
        $page->body = $validated['body'] ?? null;
        $page->meta_title = $validated['meta_title'] ?? null;
        $page->meta_description = $validated['meta_description'] ?? null;
        $page->canonical_url = $validated['canonical_url'] ?? null;
        $page->status = $validated['status'] ?? 'published';
        $page->sort_order = (int) ($validated['sort_order'] ?? 0);
        $page->is_active = (bool) ($validated['is_active'] ?? false);
        $page->is_indexable = (bool) ($validated['is_indexable'] ?? true);
        $page->save();

        $logger->log('legal_page.update', LegalPage::class, $page->id);

        return back()->with('status', 'Legal page updated.');
    }

    public function destroy(LegalPage $page, AdminActivityLogger $logger): RedirectResponse
    {
        $page->delete();
        $logger->log('legal_page.delete', LegalPage::class, $page->id);

        return back()->with('status', 'Legal page deleted.');
    }
}
