<?php

namespace App\Policies;

use App\Models\MarketingRequest;
use App\Models\User;

class MarketingRequestPolicy
{
    public function viewAny(User $user): bool
    {
        return true;
    }

    public function view(User $user, MarketingRequest $request): bool
    {
        if ($user->isSuperAdmin() || $user->isDesigner()) {
            return true;
        }

        // Harcourts Admin can view requests they submitted
        return $request->submitted_by === $user->id || $user->isHarcourtsAdmin();
    }

    public function create(User $user): bool
    {
        // Only Harcourts Admin and Super Admin can create marketing requests. Designers cannot.
        return $user->isHarcourtsAdmin() || $user->isSuperAdmin();
    }

    public function startWork(User $user, MarketingRequest $request): bool
    {
        return ($user->isDesigner() || $user->isSuperAdmin()) && in_array($request->status, ['NEW', 'IN_PROGRESS']);
    }

    public function startRevision(User $user, MarketingRequest $request): bool
    {
        return ($user->isDesigner() || $user->isSuperAdmin()) && in_array($request->status, ['CHANGES_REQUESTED', 'IN_REVISION']);
    }

    public function uploadProof(User $user, MarketingRequest $request): bool
    {
        return ($user->isDesigner() || $user->isSuperAdmin()) && in_array($request->status, ['IN_PROGRESS', 'IN_REVISION', 'AWAITING_ADMIN_REVIEW']);
    }

    public function requestChanges(User $user, MarketingRequest $request): bool
    {
        return ($user->isHarcourtsAdmin() || $user->isSuperAdmin()) && $request->status === 'AWAITING_ADMIN_REVIEW';
    }

    public function approve(User $user, MarketingRequest $request): bool
    {
        return ($user->isHarcourtsAdmin() || $user->isSuperAdmin()) && $request->status === 'AWAITING_ADMIN_REVIEW';
    }

    public function complete(User $user, MarketingRequest $request): bool
    {
        return ($user->isDesigner() || $user->isSuperAdmin() || $user->isHarcourtsAdmin()) && $request->status === 'APPROVED';
    }
}
