<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class TenantOnboardingSession extends Model
{
    /** @use HasFactory<\Database\Factories\TenantOnboardingSessionFactory> */
    use HasFactory;

    protected $table = 'managed_tenant_onboarding_sessions';

    /**
     * @var array<int, string>
     */
    public const STATE_ALLOWED_KEYS = [
        'entra_tenant_id',
        'tenant_id',
        'tenant_name',
        'environment',
        'primary_domain',
        'notes',
        'provider_connection_id',
        'selected_provider_connection_id',
        'verification_operation_run_id',
        'verification_run_id',
        'bootstrap_operation_types',
        'bootstrap_operation_runs',
        'bootstrap_run_ids',
        'connection_recently_updated',
    ];

    protected $guarded = [];

    protected $casts = [
        'state' => 'array',
        'completed_at' => 'datetime',
    ];

    /**
     * @param  array<string, mixed>|null  $value
     */
    public function setStateAttribute(?array $value): void
    {
        if ($value === null) {
            $this->attributes['state'] = null;

            return;
        }

        $allowed = array_intersect_key($value, array_flip(self::STATE_ALLOWED_KEYS));

        $encoded = json_encode($allowed, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);

        $this->attributes['state'] = $encoded !== false ? $encoded : json_encode([], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
    }

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

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

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

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