<?php

namespace App\Http\Controllers;

use App\Models\Divider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class DividerController extends Controller
{
    // Admin: Get all dividers
    public function adminIndex()
    {
        $dividers = Divider::orderBy('sort_order')->get();
        
        return response()->json([
            'success' => true,
            'data' => $dividers->map(function ($divider) {
                return [
                    'id' => $divider->id,
                    'title' => $divider->title,
                    'icon_type' => $divider->icon_type,
                    'icon' => $divider->icon,
                    'icon_image' => $divider->icon_image,
                    'icon_image_url' => $divider->icon_image_url,
                    'icon_display_url' => $divider->icon_display_url,
                    'divider_style' => $divider->divider_style,
                    'divider_color' => $divider->divider_color,
                    'divider_height' => $divider->divider_height,
                    'spacing_top' => $divider->spacing_top,
                    'spacing_bottom' => $divider->spacing_bottom,
                    'is_active' => $divider->is_active,
                    'sort_order' => $divider->sort_order,
                    'created_at' => $divider->created_at,
                ];
            }),
        ]);
    }

    // Public: Get all active dividers
    public function index()
    {
        $dividers = Divider::where('is_active', true)
            ->orderBy('sort_order')
            ->get();
        
        return response()->json([
            'success' => true,
            'data' => $dividers->map(function ($divider) {
                return [
                    'id' => $divider->id,
                    'title' => $divider->title,
                    'icon_type' => $divider->icon_type,
                    'icon' => $divider->icon,
                    'icon_display_url' => $divider->icon_display_url,
                    'divider_style' => $divider->divider_style,
                    'divider_color' => $divider->divider_color,
                    'divider_height' => $divider->divider_height,
                    'spacing_top' => $divider->spacing_top,
                    'spacing_bottom' => $divider->spacing_bottom,
                ];
            }),
        ]);
    }

    // Admin: Create divider
    public function store(Request $request)
    {
        $validated = $request->validate([
            'title' => 'nullable|string|max:255',
            'icon_type' => 'required|in:none,emoji,image',
            'icon' => 'nullable|string|max:255',
            'icon_image' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg,webp|max:51200',
            'divider_style' => 'required|in:solid,dashed,dotted,gradient',
            'divider_color' => 'required|string|max:20',
            'divider_height' => 'required|integer|min:1|max:20',
            'spacing_top' => 'required|integer|min:0|max:200',
            'spacing_bottom' => 'required|integer|min:0|max:200',
            'is_active' => 'boolean',
            'sort_order' => 'integer',
        ]);

        // Handle image upload
        if ($request->hasFile('icon_image')) {
            $validated['icon_image'] = $request->file('icon_image')->store('dividers', 'public');
        }

        $divider = Divider::create($validated);

        return response()->json([
            'success' => true,
            'data' => $divider,
            'message' => 'Divider created successfully',
        ], 201);
    }

    // Admin: Update divider
    public function update(Request $request, Divider $divider)
    {
        $validated = $request->validate([
            'title' => 'nullable|string|max:255',
            'icon_type' => 'required|in:none,emoji,image',
            'icon' => 'nullable|string|max:255',
            'icon_image' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg,webp|max:51200',
            'divider_style' => 'required|in:solid,dashed,dotted,gradient',
            'divider_color' => 'required|string|max:20',
            'divider_height' => 'required|integer|min:1|max:20',
            'spacing_top' => 'required|integer|min:0|max:200',
            'spacing_bottom' => 'required|integer|min:0|max:200',
            'is_active' => 'boolean',
            'sort_order' => 'integer',
        ]);

        // Handle image upload
        if ($request->hasFile('icon_image')) {
            // Delete old image
            if ($divider->icon_image) {
                Storage::disk('public')->delete($divider->icon_image);
            }
            $validated['icon_image'] = $request->file('icon_image')->store('dividers', 'public');
        }

        $divider->update($validated);

        return response()->json([
            'success' => true,
            'data' => $divider->fresh(),
            'message' => 'Divider updated successfully',
        ]);
    }

    // Admin: Delete divider
    public function destroy(Divider $divider)
    {
        // Delete image if exists
        if ($divider->icon_image) {
            Storage::disk('public')->delete($divider->icon_image);
        }

        $divider->delete();

        return response()->json([
            'success' => true,
            'message' => 'Divider deleted successfully',
        ]);
    }

    // Admin: Toggle active status
    public function toggleActive(Divider $divider)
    {
        $divider->is_active = !$divider->is_active;
        $divider->save();

        return response()->json([
            'success' => true,
            'data' => $divider,
            'message' => 'Status updated successfully',
        ]);
    }
}
