<?php

namespace App\Models;

use App\Enums\OrderChannel;
use App\Enums\OrderStatus;
use App\Enums\PaymentType;
use App\Enums\PriceType;
use Illuminate\Database\Eloquent\Attributes\Appends;
use Illuminate\Database\Eloquent\Attributes\Guarded;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
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\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Image\Enums\Fit;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;

#[Appends(['channel_label', 'formatted_cogs', 'formatted_discount', 'formatted_nego_price', 'payment_type_label', 'status_label', 'formatted_subtotal', 'formatted_total_amount'])]
#[Guarded(['id'])]
class Order extends Model implements HasMedia
{
    use HasFactory, InteractsWithMedia, SoftDeletes;

    protected function casts(): array
    {
        return [
            'channel' => OrderChannel::class,
            'price_type' => PriceType::class,
            'status' => OrderStatus::class,
            'payment_type' => PaymentType::class,
            'subtotal' => 'integer',
            'discount' => 'integer',
            'nego_price' => 'integer',
            'total_amount' => 'integer',
            'cogs' => 'integer',
        ];
    }

    protected function channelLabel(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->channel?->label(),
        );
    }

    protected function formattedCogs(): Attribute
    {
        return Attribute::make(
            get: fn () => 'Rp '.number_format($this->cogs, 0, ',', '.'),
        );
    }

    protected function formattedDiscount(): Attribute
    {
        return Attribute::make(
            get: fn () => 'Rp '.number_format($this->discount, 0, ',', '.'),
        );
    }

    protected function formattedNegoPrice(): Attribute
    {
        return Attribute::make(
            get: fn () => 'Rp '.number_format($this->nego_price, 0, ',', '.'),
        );
    }

    protected function paymentTypeLabel(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->payment_type?->label(),
        );
    }

    protected function statusLabel(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->status?->label(),
        );
    }

    protected function formattedSubtotal(): Attribute
    {
        return Attribute::make(
            get: fn () => 'Rp '.number_format($this->subtotal, 0, ',', '.'),
        );
    }

    protected function formattedTotalAmount(): Attribute
    {
        return Attribute::make(
            get: fn () => 'Rp '.number_format($this->total_amount, 0, ',', '.'),
        );
    }

    #[Scope]
    protected function cancelled(Builder $query): void
    {
        $query->where('status', OrderStatus::CANCELLED);
    }

    #[Scope]
    protected function cash(Builder $query): void
    {
        $query->where('payment_type', PaymentType::CASH);
    }

    #[Scope]
    protected function completed(Builder $query): void
    {
        $query->where('status', OrderStatus::COMPLETED);
    }

    #[Scope]
    protected function pending(Builder $query): void
    {
        $query->where('status', OrderStatus::PENDING);
    }

    #[Scope]
    protected function processing(Builder $query): void
    {
        $query->where('status', OrderStatus::PROCESSING);
    }

    #[Scope]
    protected function qris(Builder $query): void
    {
        $query->where('payment_type', PaymentType::QRIS);
    }

    #[Scope]
    protected function refunded(Builder $query): void
    {
        $query->where('status', OrderStatus::REFUNDED);
    }

    #[Scope]
    protected function retail(Builder $query): void
    {
        $query->where('price_type', PriceType::RETAIL);
    }

    #[Scope]
    protected function shopee(Builder $query): void
    {
        $query->where('channel', OrderChannel::SHOPEE);
    }

    #[Scope]
    protected function store(Builder $query): void
    {
        $query->where('channel', OrderChannel::STORE);
    }

    #[Scope]
    protected function tiktok(Builder $query): void
    {
        $query->where('channel', OrderChannel::TIKTOK);
    }

    #[Scope]
    protected function transfer(Builder $query): void
    {
        $query->where('payment_type', PaymentType::TRANSFER);
    }

    #[Scope]
    protected function wholesale(Builder $query): void
    {
        $query->where('price_type', PriceType::WHOLESALE);
    }

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

    public function createdBy(): BelongsTo
    {
        return $this->belongsTo(User::class, 'created_by_id');
    }

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

    public function marketing(): BelongsTo
    {
        return $this->belongsTo(User::class, 'marketing_id');
    }

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

    public function registerMediaCollections(): void
    {
        $this->addMediaCollection('photos');
    }

    public function registerMediaConversions(?Media $media = null): void
    {
        $this->addMediaConversion('thumb')
            ->fit(Fit::Contain, 150, 150);
    }
}
