<?php

declare(strict_types=1);

namespace App\Models;

use App\Support\PortfolioTriage\PortfolioArrivalContextToken;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class ManagedEnvironmentTriageReview extends Model
{
    use HasFactory;

    public const STATE_REVIEWED = 'reviewed';

    public const STATE_FOLLOW_UP_NEEDED = 'follow_up_needed';

    public const DERIVED_STATE_NOT_REVIEWED = 'not_reviewed';

    public const DERIVED_STATE_CHANGED_SINCE_REVIEW = 'changed_since_review';

    /**
     * @var list<string>
     */
    public const ACTIVE_CONCERN_FAMILIES = [
        PortfolioArrivalContextToken::FAMILY_BACKUP_HEALTH,
        PortfolioArrivalContextToken::FAMILY_RECOVERY_EVIDENCE,
    ];

    /**
     * @var list<string>
     */
    public const MANUAL_STATES = [
        self::STATE_REVIEWED,
        self::STATE_FOLLOW_UP_NEEDED,
    ];

    /**
     * @var list<string>
     */
    public const DERIVED_STATES = [
        self::DERIVED_STATE_NOT_REVIEWED,
        self::STATE_REVIEWED,
        self::STATE_FOLLOW_UP_NEEDED,
        self::DERIVED_STATE_CHANGED_SINCE_REVIEW,
    ];

    protected $guarded = [];

    /**
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'review_snapshot' => 'array',
            'reviewed_at' => 'datetime',
            'last_seen_matching_at' => 'datetime',
            'resolved_at' => 'datetime',
        ];
    }

    /**
     * @return BelongsTo<Workspace, $this>
     */
    public function workspace(): BelongsTo
    {
        return $this->belongsTo(Workspace::class);
    }

    /**
     * @return BelongsTo<ManagedEnvironment, $this>
     */
    public function tenant(): BelongsTo
    {
        return $this->belongsTo(ManagedEnvironment::class, 'managed_environment_id');
    }

    /**
     * @return BelongsTo<User, $this>
     */
    public function reviewer(): BelongsTo
    {
        return $this->belongsTo(User::class, 'reviewed_by_user_id');
    }

    /**
     * @param  Builder<self>  $query
     * @return Builder<self>
     */
    public function scopeForWorkspace(Builder $query, int $workspaceId): Builder
    {
        return $query->where('workspace_id', $workspaceId);
    }

    /**
     * @param  Builder<self>  $query
     * @return Builder<self>
     */
    public function scopeForTenant(Builder $query, int $tenantId): Builder
    {
        return $query->where('managed_environment_id', $tenantId);
    }

    /**
     * @param  Builder<self>  $query
     * @return Builder<self>
     */
    public function scopeForConcernFamily(Builder $query, string $concernFamily): Builder
    {
        return $query->where('concern_family', $concernFamily);
    }

    /**
     * @param  Builder<self>  $query
     * @return Builder<self>
     */
    public function scopeActive(Builder $query): Builder
    {
        return $query->whereNull('resolved_at');
    }

    /**
     * @param  Builder<self>  $query
     * @return Builder<self>
     */
    public function scopeResolved(Builder $query): Builder
    {
        return $query->whereNotNull('resolved_at');
    }
}
