<?php

namespace App\Policies;

use App\Models\Calendar;
use App\Models\User;
use Illuminate\Auth\Access\Response;

class CalendarPolicy
{
    /**
     * logged in users can list their calendars.
     */
    public function viewAny(User $user): bool
    {
        return true;
    }

    /**
     * users may view a calendar they own if an instances row exists
     */
    public function view(User $user, Calendar $calendar): bool
    {
        return $calendar->instances()
            ->where('principaluri', 'principals/'.$user->email)
            ->exists();
    }

    /**
     * A user may create calendars for themselves.
     */
    public function create(User $user): bool
    {
        return true;
    }

    /**
     * users may update or delete their calendar if an instance row exists
     */
    public function update(User $user, Calendar $calendar): bool
    {
        return $this->view($user, $calendar);
    }

    public function delete(User $user, Calendar $calendar): bool
    {
        return $this->view($user, $calendar);
    }

    /**
     * determine whether the user can restore the model.
     */
    public function restore(User $user, Calendar $calendar): bool
    {
        return false;
    }

    /**
     * determine whether the user can permanently delete the model.
     */
    public function forceDelete(User $user, Calendar $calendar): bool
    {
        return false;
    }
}
