<?php

namespace App\Http\Controllers;

use App\Http\Requests\StoreTopbarNotificationRequest;
use App\Http\Requests\UpdateTopbarNotificationRequest;
use App\Models\TopbarNotification;

class TopbarNotificationController extends Controller
{
    /**
     * Display a paginated list of all topbar notifications.
     * Requirements: 4.1, 4.2, 4.3
     */
    public function index()
    {
        abort_unless(auth()->user()->can('view topbar notifications'), 403);

        $notifications = TopbarNotification::orderBy('order')->paginate(15);

        return view('topbar-notifications.index', compact('notifications'));
    }

    /**
     * Show the form for creating a new notification.
     * Requirements: 4.1
     */
    public function create()
    {
        abort_unless(auth()->user()->can('create topbar notifications'), 403);

        return view('topbar-notifications.create');
    }

    /**
     * Store a newly created notification.
     * Requirements: 4.1, 4.4, 4.5
     */
    public function store(StoreTopbarNotificationRequest $request)
    {
        $data = $request->validated();
        $data['is_active'] = $request->boolean('is_active', true);

        TopbarNotification::create($data);

        return redirect()
            ->route('topbar-notifications.index')
            ->with('success', 'Topbar notification created successfully.');
    }

    /**
     * Show the form for editing the specified notification.
     * Requirements: 4.1
     */
    public function edit(TopbarNotification $topbarNotification)
    {
        abort_unless(auth()->user()->can('edit topbar notifications'), 403);

        return view('topbar-notifications.edit', compact('topbarNotification'));
    }

    /**
     * Update the specified notification.
     * Requirements: 4.1, 4.4, 4.6
     */
    public function update(UpdateTopbarNotificationRequest $request, TopbarNotification $topbarNotification)
    {
        $data = $request->validated();
        $data['is_active'] = $request->boolean('is_active');

        $topbarNotification->update($data);

        return redirect()
            ->route('topbar-notifications.index')
            ->with('success', 'Topbar notification updated successfully.');
    }

    /**
     * Delete the specified notification.
     * Requirements: 4.1, 4.7
     */
    public function destroy(TopbarNotification $topbarNotification)
    {
        abort_unless(auth()->user()->can('delete topbar notifications'), 403);

        $topbarNotification->delete();

        return redirect()
            ->route('topbar-notifications.index')
            ->with('success', 'Topbar notification deleted successfully.');
    }

    /**
     * Display the specified notification on the public website.
     * Requirements: 4.8, 4.9
     * Public route - no authentication required.
     */
    public function show(TopbarNotification $topbarNotification)
    {
        // Only show active notifications or future notifications
        if (!$topbarNotification->is_active) {
            abort(404, 'This notification is no longer available.');
        }

        // If end_date has passed, it's expired
        if ($topbarNotification->end_date && $topbarNotification->end_date->isPast()) {
            abort(404, 'This notification has expired.');
        }

        // If start_date is in the future, it's not yet available
        if ($topbarNotification->start_date && $topbarNotification->start_date->isFuture()) {
            abort(404, 'This notification is not yet available.');
        }

        return view('topbar-notifications.show', compact('topbarNotification'));
    }
}
