<?php

namespace App\Http\Controllers\Tenant;

use App\Http\Controllers\Controller;
use App\Http\Requests\ScheduleExceptionRequest;
use App\Models\Dentist;
use App\Models\ScheduleException;

class ScheduleExceptionController extends Controller
{
    public function index()
    {
        $this->authorize('viewAny', Dentist::class);

        return view('dentists.exceptions', [
            'exceptions' => ScheduleException::with('dentist')
                ->whereDate('date', '>=', today()->subDays(7))
                ->orderBy('date')
                ->paginate(30),
            'dentists' => Dentist::active()->orderBy('name')->get(),
        ]);
    }

    public function store(ScheduleExceptionRequest $request)
    {
        ScheduleException::create($request->validated());

        return back()->with('status', __('Schedule exception added.'));
    }

    public function destroy(ScheduleException $exception)
    {
        abort_unless($exception->tenant_id === app(\App\Support\Tenancy::class)->id(), 404);
        abort_unless(auth()->user()->can('appointments.manage'), 403);

        $exception->delete();

        return back()->with('status', __('Schedule exception removed.'));
    }
}
