<?php

declare(strict_types=1);

namespace App\Models\List\External;

use App\Models\Auth\User;
use App\Models\BaseModel;
use App\Models\List\ExternalProfile;
use Database\Factories\List\External\ExternalTokenFactory;
use Illuminate\Database\Eloquent\Attributes\Hidden;
use Illuminate\Database\Eloquent\Attributes\Table;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Znck\Eloquent\Relations\BelongsToThrough;
use Znck\Eloquent\Traits\BelongsToThrough as TraitsBelongsToThrough;

/**
 * @property int $token_id
 * @property int $profile_id
 * @property string|null $access_token
 * @property ExternalProfile $externalprofile
 * @property int $profile_id
 * @property string|null $refresh_token
 * @property User $user
 *
 * @method static ExternalTokenFactory factory(...$parameters)
 */
#[Hidden([
    ExternalToken::ATTRIBUTE_ACCESS_TOKEN,
    ExternalToken::ATTRIBUTE_REFRESH_TOKEN,
])]
#[Table(ExternalToken::TABLE, ExternalToken::ATTRIBUTE_ID)]
class ExternalToken extends BaseModel
{
    use HasFactory;
    use TraitsBelongsToThrough;

    final public const string TABLE = 'external_tokens';

    final public const string ATTRIBUTE_ID = 'token_id';
    final public const string ATTRIBUTE_PROFILE = 'profile_id';
    final public const string ATTRIBUTE_ACCESS_TOKEN = 'access_token';
    final public const string ATTRIBUTE_REFRESH_TOKEN = 'refresh_token';

    final public const string RELATION_PROFILE = 'externalprofile';
    final public const string RELATION_USER = 'externalprofile.user';
    final public const string RELATION_USER_SHALLOW = 'user';

    /**
     * The attributes that are mass assignable.
     *
     * @var list<string>
     */
    protected $fillable = [
        ExternalToken::ATTRIBUTE_ACCESS_TOKEN,
        ExternalToken::ATTRIBUTE_PROFILE,
        ExternalToken::ATTRIBUTE_REFRESH_TOKEN,
    ];

    /**
     * Get the attributes that should be cast.
     *
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            ExternalToken::ATTRIBUTE_ACCESS_TOKEN => 'string',
            ExternalToken::ATTRIBUTE_PROFILE => 'int',
            ExternalToken::ATTRIBUTE_REFRESH_TOKEN => 'string',
        ];
    }

    public function getName(): string
    {
        return strval($this->getKey());
    }

    public function getSubtitle(): string
    {
        return $this->externalprofile->getName();
    }

    /**
     * @return BelongsTo<ExternalProfile, $this>
     */
    public function externalprofile(): BelongsTo
    {
        return $this->belongsTo(ExternalProfile::class, ExternalToken::ATTRIBUTE_PROFILE);
    }

    /**
     * Get the user that owns the external token through the external profile.
     */
    public function user(): BelongsToThrough
    {
        return $this->belongsToThrough(
            User::class,
            ExternalProfile::class,
            null,
            '',
            [
                User::class => ExternalProfile::ATTRIBUTE_USER,
                ExternalProfile::class => ExternalProfile::ATTRIBUTE_ID,
            ]
        );
    }
}
