<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Promotion;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class PromotionController extends Controller
{
    public function index()
    {
        $promotions = Promotion::orderBy('sort_order')->orderBy('created_at', 'desc')->get();
        return view('admin.promotions.index', compact('promotions'));
    }

    public function create()
    {
        return view('admin.promotions.create');
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'title'          => 'required|string|max:255',
            'subtitle'       => 'nullable|string|max:255',
            'description'    => 'nullable|string|max:500',
            'discount_text'  => 'nullable|string|max:50',
            'image'          => 'nullable|image|max:4096',
            'link_url'       => 'nullable|url|max:500',
            'button_label'   => 'nullable|string|max:100',
            'is_active'      => 'boolean',
            'sort_order'     => 'integer|min:0',
            'starts_at'      => 'nullable|date',
            'ends_at'        => 'nullable|date|after_or_equal:starts_at',
        ]);

        if ($request->hasFile('image')) {
            $data['image'] = $request->file('image')->store('promotions', 'public');
        }

        $data['is_active']    = $request->boolean('is_active');
        $data['button_label'] = $data['button_label'] ?: "Profiter de l'offre";

        Promotion::create($data);

        return redirect()->route('admin.promotions.index')
                         ->with('success', 'Offre spéciale créée avec succès.');
    }

    public function edit(Promotion $promotion)
    {
        return view('admin.promotions.edit', compact('promotion'));
    }

    public function update(Request $request, Promotion $promotion)
    {
        $data = $request->validate([
            'title'          => 'required|string|max:255',
            'subtitle'       => 'nullable|string|max:255',
            'description'    => 'nullable|string|max:500',
            'discount_text'  => 'nullable|string|max:50',
            'image'          => 'nullable|image|max:4096',
            'link_url'       => 'nullable|url|max:500',
            'button_label'   => 'nullable|string|max:100',
            'is_active'      => 'boolean',
            'sort_order'     => 'integer|min:0',
            'starts_at'      => 'nullable|date',
            'ends_at'        => 'nullable|date|after_or_equal:starts_at',
        ]);

        if ($request->hasFile('image')) {
            if ($promotion->image && !str_starts_with($promotion->image, 'http')) {
                Storage::disk('public')->delete($promotion->image);
            }
            $data['image'] = $request->file('image')->store('promotions', 'public');
        }

        $data['is_active']    = $request->boolean('is_active');
        $data['button_label'] = $data['button_label'] ?: "Profiter de l'offre";

        $promotion->update($data);

        return redirect()->route('admin.promotions.index')
                         ->with('success', 'Offre spéciale mise à jour.');
    }

    public function destroy(Promotion $promotion)
    {
        if ($promotion->image && !str_starts_with($promotion->image, 'http')) {
            Storage::disk('public')->delete($promotion->image);
        }
        $promotion->delete();

        return redirect()->route('admin.promotions.index')
                         ->with('success', 'Offre supprimée.');
    }

    public function toggleActive(Promotion $promotion)
    {
        $promotion->update(['is_active' => !$promotion->is_active]);
        return back()->with('success', 'Statut mis à jour.');
    }
}
