<?php

declare(strict_types=1);

namespace App\Models;

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

final class ReviewedWebsiteReview extends Model
{
    /** @use HasFactory<\Database\Factories\ReviewedWebsiteReviewFactory> */
    use HasFactory;

    public const STATUS_PENDING = 'pending';

    public const STATUS_APPROVED = 'approved';

    public const STATUS_REJECTED = 'rejected';

    public const VERDICT_LEGIT = 'legit';

    public const VERDICT_SCAM = 'scam';

    public const VERDICT_UNSURE = 'unsure';

    protected $fillable = [
        'reviewed_website_id',
        'user_id',
        'guest_name',
        'guest_email',
        'rating',
        'user_verdict',
        'body',
        'status',
    ];

    protected function casts(): array
    {
        return [
            'reviewed_website_id' => 'integer',
            'user_id' => 'integer',
            'rating' => 'integer',
            'created_at' => 'datetime',
            'updated_at' => 'datetime',
        ];
    }

    public function reviewerDisplayName(): string
    {
        if ($this->user_id !== null) {
            return $this->user?->name ?? 'Reader';
        }

        return $this->guest_name ?? 'Reader';
    }

    /**
     * @return BelongsTo<ReviewedWebsite, $this>
     */
    public function reviewedWebsite(): BelongsTo
    {
        return $this->belongsTo(ReviewedWebsite::class, 'reviewed_website_id');
    }

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

    /**
     * @return array<string, string>
     */
    public static function userVerdictLabels(): array
    {
        return [
            self::VERDICT_LEGIT => 'Legit',
            self::VERDICT_SCAM => 'Scam',
            self::VERDICT_UNSURE => 'Unsure',
        ];
    }
}
