<?php

namespace App\Http\Controllers;

use App\Models\BrandShowcase;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
use App\Traits\FileUploader;

class BrandShowcaseController extends Controller
{
    use FileUploader;

    // Public: Get active brand showcases
    public function index()
    {
        $brands = BrandShowcase::with('brand')
            ->active()
            ->orderBy('sort_order')
            ->get();

        return response()->json([
            'message' => 'برندهای نمایشی',
            'data' => $brands
        ]);
    }

    // Admin: Get all brand showcases
    public function adminIndex(Request $request)
    {
        $query = BrandShowcase::with('brand')->orderBy('sort_order');

        if ($request->has('active')) {
            $query->where('is_active', $request->active);
        }

        $brands = $query->get();

        return response()->json([
            'message' => 'لیست برندها',
            'data' => $brands
        ]);
    }

    // Admin: Get single brand showcase
    public function show($id)
    {
        $brand = BrandShowcase::with('brand')->findOrFail($id);

        return response()->json([
            'message' => 'جزئیات برند',
            'data' => $brand
        ]);
    }

    // Admin: Create brand showcase
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'required|string|max:255',
            'brand_id' => 'nullable|exists:brands,id',
            'logo' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg,webp|max:512000',
            'link' => 'nullable|url|max:500',
            'description' => 'nullable|string',
            'sort_order' => 'integer',
            'is_active' => 'boolean',
        ]);

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

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

        // Upload logo
        if ($request->hasFile('logo')) {
            $data['logo'] = $this->uploadImage($request->file('logo'), 'brands');
        }

        $brand = BrandShowcase::create($data);

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

    // Admin: Update brand showcase
    public function update(Request $request, $id)
    {
        $brand = BrandShowcase::findOrFail($id);

        $validator = Validator::make($request->all(), [
            'name' => 'required|string|max:255',
            'brand_id' => 'nullable|exists:brands,id',
            'logo' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg,webp|max:512000',
            'link' => 'nullable|url|max:500',
            'description' => 'nullable|string',
            'sort_order' => 'integer',
            'is_active' => 'boolean',
        ]);

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

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

        // Upload new logo
        if ($request->hasFile('logo')) {
            // Delete old logo
            if ($brand->logo) {
                Storage::delete($brand->logo);
            }
            $data['logo'] = $this->uploadImage($request->file('logo'), 'brands');
        }

        $brand->update($data);

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

    // Admin: Delete brand showcase
    public function destroy($id)
    {
        $brand = BrandShowcase::findOrFail($id);

        // Delete logo
        if ($brand->logo) {
            Storage::delete($brand->logo);
        }

        $brand->delete();

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