<?php

namespace Modules\Admin\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\Admin\Services\WeeklySpecialService;

class WeeklySpecialController extends Controller
{
    public function __construct(
        private WeeklySpecialService $weeklySpecialService
    ) {}

    public function index()
    {
        $weeklySpecials = $this->weeklySpecialService->getAll();
        return view('admin::weekly-specials.index', compact('weeklySpecials'));
    }

    public function show(int $id)
    {
        $weeklySpecial = $this->weeklySpecialService->findById($id);
        if (!$weeklySpecial) {
            return redirect()->route('admin.weekly-specials.index')->with('error', 'Weekly special not found.');
        }
        return view('admin::weekly-specials.show', compact('weeklySpecial'));
    }

    public function create()
    {
        return view('admin::weekly-specials.create');
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'name' => 'required|string|max:255',
            'slug' => 'required|string|unique:weekly_specials,slug',
            'description' => 'nullable|string',
            'icon' => 'nullable|string',
            'banner_url' => 'nullable|string',
            'max_discount' => 'nullable|numeric|min:0|max:100',
            'starts_at' => 'required|date',
            'ends_at' => 'required|date|after_or_equal:starts_at',
            'is_active' => 'boolean',
        ]);

        $this->weeklySpecialService->create($data);

        return redirect()->route('admin.weekly-specials.index')
            ->with('success', 'Weekly special created successfully.');
    }

    public function edit(int $id)
    {
        $weeklySpecial = $this->weeklySpecialService->findById($id);
        if (!$weeklySpecial) {
            return redirect()->route('admin.weekly-specials.index')->with('error', 'Weekly special not found.');
        }
        return view('admin::weekly-specials.edit', compact('weeklySpecial'));
    }

    public function update(Request $request, int $id)
    {
        $data = $request->validate([
            'name' => 'string|max:255',
            'slug' => 'string|unique:weekly_specials,slug,' . $id,
            'description' => 'nullable|string',
            'icon' => 'nullable|string',
            'banner_url' => 'nullable|string',
            'max_discount' => 'nullable|numeric|min:0|max:100',
            'starts_at' => 'date',
            'ends_at' => 'date|after_or_equal:starts_at',
            'is_active' => 'boolean',
        ]);

        $this->weeklySpecialService->update($id, $data);

        return redirect()->route('admin.weekly-specials.index')
            ->with('success', 'Weekly special updated successfully.');
    }

    public function destroy(int $id)
    {
        $this->weeklySpecialService->delete($id);
        return redirect()->route('admin.weekly-specials.index')
            ->with('success', 'Weekly special deleted successfully.');
    }
}
