<?php

namespace AleshaCMS\Http\Controllers\Admin;

use AleshaCMS\Http\Controllers\Controller;
use AleshaCMS\Models\CMS\ContentType;
use AleshaCMS\CMS\Content\ContentTypeManager;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class ContentTypeController extends Controller
{
    public function index(Request $request)
    {
        $types = ContentType::all();

        if ($request->wantsJson()) {
            return response()->json([
                'data' => $types->map(fn($t) => [
                    'id' => $t->id,
                    'name' => $t->name,
                    'slug' => $t->slug,
                    'icon' => $t->icon ?? 'squares-2x2',
                    'is_active' => $t->is_active,
                    'fields_count' => count($t->fields ?? []),
                    'rewrite_slug' => $t->rewrite_slug ?? $t->slug,
                    'manage_url' => route('admin.posts.index', $t->slug),
                    'edit_url' => route('admin.content-types.edit', $t->id),
                ]),
            ]);
        }

        return view('admin.content-types.index', compact('types'));
    }

    public function create()
    {
        return view('admin.content-types.create');
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'name' => 'required|string|max:100',
            'slug' => 'nullable|string|max:100|unique:content_types',
            'icon' => 'nullable|string|max:50',
            'fields' => 'nullable|array',
            'has_archive' => 'boolean',
            'rewrite_slug' => 'nullable|string|max:100',
        ]);

        $data['slug'] = $data['slug'] ?? Str::slug($data['name']);
        $type = ContentType::create($data);
        app(ContentTypeManager::class)->clearCache();

        if ($request->wantsJson()) {
            return response()->json([
                'success' => true,
                'message' => 'Content type created.',
                'redirect' => route('admin.content-types.index'),
            ]);
        }

        return redirect()->route('admin.content-types.index')->with('success', 'Content type created.');
    }

    public function edit(ContentType $contentType)
    {
        return view('admin.content-types.edit', compact('contentType'));
    }

    public function update(Request $request, ContentType $contentType)
    {
        $data = $request->validate([
            'name' => 'required|string|max:100',
            'icon' => 'nullable|string|max:50',
            'fields' => 'nullable|array',
            'has_archive' => 'boolean',
            'rewrite_slug' => 'nullable|string|max:100',
            'is_active' => 'boolean',
        ]);

        $contentType->update($data);
        app(ContentTypeManager::class)->clearCache();

        if ($request->wantsJson()) {
            return response()->json(['success' => true, 'message' => 'Content type updated.']);
        }

        return back()->with('success', 'Content type updated.');
    }

    public function destroy(ContentType $contentType)
    {
        $contentType->delete();
        app(ContentTypeManager::class)->clearCache();

        if (request()->wantsJson()) {
            return response()->json(['success' => true, 'message' => 'Content type deleted.']);
        }

        return redirect()->route('admin.content-types.index')->with('success', 'Content type deleted.');
    }
}
