<?php

namespace App\Services\Compliance;

use App\Enums\RetentionOverrideScope;
use App\Models\Event;
use App\Models\RetentionOverride;
use App\Models\Tenant;

class RetentionOverrideService
{
    public function tenantOnHold(Tenant $tenant): bool
    {
        return RetentionOverride::query()
            ->where('scope', RetentionOverrideScope::TENANT->value)
            ->where('tenant_id', $tenant->id)
            ->whereNull('released_at')
            ->exists();
    }

    public function eventOnHold(Event $event): bool
    {
        return RetentionOverride::query()
            ->whereNull('released_at')
            ->where(function ($query) use ($event) {
                $query->where(function ($inner) use ($event) {
                    $inner->where('scope', RetentionOverrideScope::EVENT->value)
                        ->where('event_id', $event->id);
                })->orWhere(function ($inner) use ($event) {
                    $inner->where('scope', RetentionOverrideScope::TENANT->value)
                        ->where('tenant_id', $event->tenant_id);
                });
            })
            ->exists();
    }
}
