<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Facades\Log;

class Tenant extends Model
{
    use HasFactory;

    protected $table = 'tenants';

    protected $guarded = [];

    protected $casts = [
        'features' => 'array',
        'settings' => 'array',
        'notification_preferences' => 'array',
        'last_activity_at' => 'datetime',
        'total_revenue' => 'decimal:2',
        'settings_updated_at' => 'datetime',
        'subscription_expires_at' => 'datetime',
        'credit_warning_sent_at' => 'datetime',
        'credit_warning_threshold' => 'integer',
    ];

    public function events(): HasMany
    {
        return $this->hasMany(Event::class);
    }

    public function photos(): HasManyThrough
    {
        return $this->hasManyThrough(
            Photo::class,
            Event::class,
            'tenant_id',
            'event_id',
            'id',
            'id'
        );
    }

    public function members(): HasMany
    {
        return $this->hasMany(EventMember::class);
    }

    public function purchases(): HasMany
    {
        return $this->hasMany(PackagePurchase::class);
    }

    public function tenantPackages(): HasMany
    {
        return $this->hasMany(TenantPackage::class);
    }

    public function packages(): BelongsToMany
    {
        return $this->belongsToMany(Package::class, 'tenant_packages')
            ->withPivot(['price', 'purchased_at', 'expires_at', 'active'])
            ->withTimestamps();
    }

    public function activeResellerPackage(): HasOne
    {
        return $this->hasOne(TenantPackage::class)->where('active', true);
    }

    public function notificationLogs(): HasMany
    {
        return $this->hasMany(TenantNotificationLog::class);
    }

    public function canCreateEvent(): bool
    {
        return $this->hasEventAllowance();
    }

    public function incrementUsedEvents(int $amount = 1): bool
    {
        $package = $this->getActiveResellerPackage();
        if (! $package) {
            return false;
        }

        $package->increment('used_events', $amount);

        return true;
    }

    public function setSettingsAttribute($value): void
    {
        if (is_string($value)) {
            $this->attributes['settings'] = $value;

            return;
        }

        $this->attributes['settings'] = json_encode($value ?? []);
    }

    public function hasEventAllowance(): bool
    {
        $package = $this->getActiveResellerPackage();
        if ($package && $package->canCreateEvent()) {
            return true;
        }

        return false;
    }

    public function consumeEventAllowance(int $amount = 1, string $reason = 'event.create', ?string $note = null): bool
    {
        $package = $this->getActiveResellerPackage();
        if ($package && $package->canCreateEvent()) {
            $previousUsed = (int) $package->used_events;
            $package->increment('used_events', $amount);
            $package->refresh();

            app(\App\Services\Packages\TenantUsageTracker::class)->recordEventUsage(
                $package,
                $previousUsed,
                $amount
            );

            Log::info('Tenant package usage recorded', [
                'tenant_id' => $this->id,
                'tenant_package_id' => $package->id,
                'used_events' => $package->used_events,
                'amount' => $amount,
            ]);

            return true;
        }

        Log::warning('Event allowance missing for tenant', [
            'tenant_id' => $this->id,
            'reason' => $reason,
        ]);

        return false;
    }

    public function getActiveResellerPackage(): ?TenantPackage
    {
        return $this->activeResellerPackage()
            ->whereHas('package', fn ($query) => $query->withTrashed()->where('type', 'reseller'))
            ->where('active', true)
            ->orderByDesc('expires_at')
            ->first();
    }

    public function activeSubscription(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->activeResellerPackage()->exists(),
        );
    }

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