<?php

declare(strict_types=1);

namespace ImovelZapAi\Organizations\Infrastructure\Persistence\Eloquent;

use Database\Factories\OrganizationSubscriptionFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use ImovelZapAi\Organizations\Domain\Enums\SubscriptionStatus;
use ImovelZapAi\Shared\Infrastructure\Persistence\Concerns\HasUuidPrimaryKey;
use ImovelZapAi\Tenancy\Infrastructure\Persistence\Eloquent\TenantModel;

class OrganizationSubscriptionModel extends Model
{
    /** @use HasFactory<OrganizationSubscriptionFactory> */
    use HasFactory;
    use HasUuidPrimaryKey;

    protected $table = 'organization_subscriptions';

    /** @var list<string> */
    protected $fillable = [
        'tenant_id',
        'subscription_plan_id',
        'status',
        'billing_cycle',
        'mp_preapproval_id',
        'mp_payer_email',
        'coupon_code',
        'trial_ends_at',
        'current_period_ends_at',
        'canceled_at',
        'grace_ends_at',
    ];

    /** @return array<string, string> */
    protected function casts(): array
    {
        return [
            'status' => SubscriptionStatus::class,
            'trial_ends_at' => 'datetime',
            'current_period_ends_at' => 'datetime',
            'canceled_at' => 'datetime',
            'grace_ends_at' => 'datetime',
        ];
    }

    public function tenant(): BelongsTo
    {
        return $this->belongsTo(TenantModel::class, 'tenant_id');
    }

    public function plan(): BelongsTo
    {
        return $this->belongsTo(SubscriptionPlanModel::class, 'subscription_plan_id');
    }

    protected static function newFactory(): OrganizationSubscriptionFactory
    {
        return OrganizationSubscriptionFactory::new();
    }
}
