<?php

namespace App\Domain\Pricing\Models;

use App\Domain\Pricing\Enums\AdjustmentType;
use App\Domain\Pricing\Enums\PricingRuleType;
use App\Domain\Shared\Traits\BelongsToAcademy;
use App\Domain\Shared\Traits\HasUuid;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;

class PricingRule extends Model
{
    use BelongsToAcademy, HasUuid, SoftDeletes;

    protected $fillable = [
        'academy_id',
        'name_ar',
        'name',
        'description_ar',
        'rule_type',
        'adjustment_type',
        'adjustment_value',
        'conditions',
        'target_type',
        'target_id',
        'branch_id',
        'priority',
        'is_stackable',
        'max_discount_percent',
        'effective_from',
        'effective_to',
        'is_active',
        'usage_limit',
        'usage_count',
        'created_by',
        'metadata',
    ];

    protected $casts = [
        'rule_type' => PricingRuleType::class,
        'adjustment_type' => AdjustmentType::class,
        'adjustment_value' => 'integer',
        'conditions' => 'array',
        'is_stackable' => 'boolean',
        'is_active' => 'boolean',
        'max_discount_percent' => 'integer',
        'priority' => 'integer',
        'usage_limit' => 'integer',
        'usage_count' => 'integer',
        'effective_from' => 'date',
        'effective_to' => 'date',
        'metadata' => 'array',
    ];

    // ─── Relationships ────────────────────────────────────────────────

    public function branch(): BelongsTo
    {
        return $this->belongsTo(\App\Domain\Identity\Models\Branch::class);
    }

    public function creator(): BelongsTo
    {
        return $this->belongsTo(\App\Models\User::class, 'created_by');
    }

    // ─── Scopes ───────────────────────────────────────────────────────

    public function scopeActive(Builder $query): Builder
    {
        return $query->where('is_active', true);
    }

    public function scopeEffectiveOn(Builder $query, $date): Builder
    {
        return $query->where(function (Builder $q) use ($date) {
            $q->whereNull('effective_from')
                ->orWhere('effective_from', '<=', $date);
        })->where(function (Builder $q) use ($date) {
            $q->whereNull('effective_to')
                ->orWhere('effective_to', '>=', $date);
        });
    }

    public function scopeForBranch(Builder $query, ?int $branchId): Builder
    {
        return $query->where(function (Builder $q) use ($branchId) {
            $q->whereNull('branch_id')
                ->orWhere('branch_id', $branchId);
        });
    }

    public function scopeForTarget(Builder $query, ?string $type, ?int $id = null): Builder
    {
        return $query->where(function (Builder $q) use ($type, $id) {
            $q->whereNull('target_type')
                ->orWhere(function (Builder $q2) use ($type, $id) {
                    $q2->where('target_type', $type);
                    if ($id !== null) {
                        $q2->where(function (Builder $q3) use ($id) {
                            $q3->whereNull('target_id')
                                ->orWhere('target_id', $id);
                        });
                    }
                });
        });
    }

    public function scopeStackable(Builder $query): Builder
    {
        return $query->where('is_stackable', true);
    }

    public function scopeNonStackable(Builder $query): Builder
    {
        return $query->where('is_stackable', false);
    }
}
