<?php

namespace App\Http\Controllers;

use App\Models\CustomCard;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;

class CustomCardController extends Controller
{
    // Public: Get all active cards
    public function index()
    {
        $cards = CustomCard::active()->ordered()->get();

        return response()->json([
            'message' => 'کارت‌های سفارشی',
            'data' => $cards
        ]);
    }

    // Admin: Get all cards
    public function adminIndex()
    {
        $cards = CustomCard::ordered()->get();

        return response()->json([
            'message' => 'لیست کارت‌ها',
            'data' => $cards
        ]);
    }

    // Admin: Create card
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'layout_id' => 'required|exists:custom_card_layouts,id',
            'title' => 'nullable|string|max:255',
            'description' => 'nullable|string',
            'image' => 'nullable|file|mimes:jpeg,png,webp,gif|max:51200',
            'bg_color' => 'nullable|string|max:20',
            'text_color' => 'nullable|string|max:20',
            'col_span' => 'required|integer|min:1|max:12',
            'row_span' => 'required|integer|min:1|max:24',
            'link_type' => 'required|in:none,internal,external,category,product',
            'link_url' => 'nullable|string|max:500',
            'link_id' => 'nullable|integer',
            'show_button' => 'boolean',
            'button_text' => 'nullable|string|max:100',
            'button_link' => 'nullable|string|max:500',
            'is_active' => 'boolean',
            'sort_order' => 'integer',
        ]);

        if ($validator->fails()) {
            return response()->json([
                'message' => 'اطلاعات نامعتبر است',
                'errors' => $validator->errors()
            ], 422);
        }

        $data = $request->except('image');

        // Handle image upload
        if ($request->hasFile('image')) {
            $data['image'] = $request->file('image')->store('custom-cards', 'public');
        }

        $card = CustomCard::create($data);

        return response()->json([
            'message' => 'کارت با موفقیت ایجاد شد',
            'data' => $card
        ], 201);
    }

    // Admin: Update card
    public function update(Request $request, $id)
    {
        $card = CustomCard::findOrFail($id);

        $validator = Validator::make($request->all(), [
            'layout_id' => 'sometimes|exists:custom_card_layouts,id',
            'title' => 'nullable|string|max:255',
            'description' => 'nullable|string',
            'image' => 'nullable|file|mimes:jpeg,png,webp,gif|max:51200',
            'bg_color' => 'nullable|string|max:20',
            'text_color' => 'nullable|string|max:20',
            'col_span' => 'sometimes|integer|min:1|max:12',
            'row_span' => 'sometimes|integer|min:1|max:24',
            'link_type' => 'sometimes|in:none,internal,external,category,product',
            'link_url' => 'nullable|string|max:500',
            'link_id' => 'nullable|integer',
            'show_button' => 'boolean',
            'button_text' => 'nullable|string|max:100',
            'button_link' => 'nullable|string|max:500',
            'is_active' => 'boolean',
            'sort_order' => 'integer',
        ]);

        if ($validator->fails()) {
            return response()->json([
                'message' => 'اطلاعات نامعتبر است',
                'errors' => $validator->errors()
            ], 422);
        }

        $data = $request->except('image');

        // Handle image upload
        if ($request->hasFile('image')) {
            if ($card->image) {
                Storage::disk('public')->delete($card->image);
            }
            $data['image'] = $request->file('image')->store('custom-cards', 'public');
        }

        $card->update($data);

        return response()->json([
            'message' => 'کارت با موفقیت ویرایش شد',
            'data' => $card
        ]);
    }

    // Admin: Delete card
    public function destroy($id)
    {
        $card = CustomCard::findOrFail($id);

        if ($card->image) {
            Storage::disk('public')->delete($card->image);
        }

        $card->delete();

        return response()->json([
            'message' => 'کارت حذف شد'
        ]);
    }

    // Admin: Toggle active
    public function toggleActive($id)
    {
        $card = CustomCard::findOrFail($id);
        $card->update(['is_active' => !$card->is_active]);

        return response()->json([
            'message' => 'وضعیت نمایش تغییر کرد',
            'data' => $card
        ]);
    }
}
