<?php

declare(strict_types=1);

namespace App\Models;

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

/**
 * @property int              $id
 * @property int              $user_id
 * @property string|null      $text
 * @property string|null      $source_hash
 * @property string|null      $model
 * @property string|null      $prompt_version
 * @property string|null      $input_quality_tier
 * @property string|null      $generation_reason
 * @property string           $status
 * @property bool             $is_active
 * @property bool             $is_hidden
 * @property bool             $is_user_edited
 * @property bool             $needs_review
 * @property \Carbon\Carbon|null $generated_at
 * @property \Carbon\Carbon|null $approved_at
 * @property \Carbon\Carbon|null $last_attempted_at
 * @property string|null      $last_error_code
 * @property string|null      $last_error_reason
 * @property \Carbon\Carbon   $created_at
 * @property \Carbon\Carbon   $updated_at
 */
final class CreatorAiBiography extends Model
{
    public const STATUS_GENERATED     = 'generated';
    public const STATUS_APPROVED      = 'approved';
    public const STATUS_EDITED        = 'edited';
    public const STATUS_HIDDEN        = 'hidden';
    public const STATUS_FAILED        = 'failed';
    public const STATUS_NEEDS_REVIEW  = 'needs_review';
    public const STATUS_SUPPRESSED    = 'suppressed_low_signal';

    public const TIER_RICH   = 'rich';
    public const TIER_MEDIUM = 'medium';
    public const TIER_SPARSE = 'sparse';

    public const REASON_INITIAL_GENERATE        = 'initial_generate';
    public const REASON_MANUAL_REGENERATE       = 'manual_regenerate';
    public const REASON_STALE_REFRESH           = 'stale_refresh';
    public const REASON_MILESTONE_CHANGE        = 'milestone_change';
    public const REASON_ERA_CHANGE              = 'era_change';
    public const REASON_FEATURED_CHANGE         = 'featured_change';
    public const REASON_ADMIN_BATCH             = 'admin_batch';
    public const REASON_RETRY_AFTER_FAILURE     = 'retry_after_validation_failure';

    protected $table = 'creator_ai_biographies';

    protected $fillable = [
        'user_id',
        'text',
        'source_hash',
        'model',
        'prompt_version',
        'input_quality_tier',
        'generation_reason',
        'status',
        'is_active',
        'is_hidden',
        'is_user_edited',
        'needs_review',
        'generated_at',
        'approved_at',
        'last_attempted_at',
        'last_error_code',
        'last_error_reason',
    ];

    protected $casts = [
        'is_active'        => 'boolean',
        'is_hidden'        => 'boolean',
        'is_user_edited'   => 'boolean',
        'needs_review'     => 'boolean',
        'generated_at'     => 'datetime',
        'approved_at'      => 'datetime',
        'last_attempted_at' => 'datetime',
    ];

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    public function isVisible(): bool
    {
        return $this->is_active
            && ! $this->is_hidden
            && in_array($this->status, [self::STATUS_GENERATED, self::STATUS_APPROVED, self::STATUS_EDITED, self::STATUS_NEEDS_REVIEW], true)
            && $this->text !== null
            && trim($this->text) !== '';
    }
}
