<?php

namespace App\Policies;

use App\Models\User;
use App\Models\LeasingRecord;

class LeasingRecordPolicy
{
    public function before(User $user, $ability)
    {
        if ($user->isAdmin()) return true;
    }

    public function viewAny(User $user)
    {
        return $user->isAdmin() || $user->isLeasing();
    }

    public function view(User $user, LeasingRecord $record)
    {
        // Leasing owner can view
        if ($user->isLeasing() && optional($user->leasingProfile)->id === $record->leasing_profile_id) {
            return true;
        }

        // Member can only view if status is active
        if ($user->isMember() && $record->record_status === 'active') {
            return true;
        }

        return false;
    }

    public function create(User $user)
    {
        return $user->isLeasing();
    }

    public function update(User $user, LeasingRecord $record)
    {
        return $user->isLeasing() && optional($user->leasingProfile)->id === $record->leasing_profile_id;
    }

    public function delete(User $user, LeasingRecord $record)
    {
        return $user->isLeasing() && optional($user->leasingProfile)->id === $record->leasing_profile_id;
    }
}
