<?php

namespace App\Models;

use App\Enums\OrderChannel;
use App\Enums\OrderStatus;
use Dyrynda\Database\Support\CascadeSoftDeletes;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
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 Veelasky\LaravelHashId\Eloquent\HashableId;

class Order extends Model
{
    use CascadeSoftDeletes, HasFactory, HashableId, SoftDeletes;

    protected $guarded = ['id'];

    protected $cascadeDeletes = ['items', 'payments'];

    protected function casts(): array
    {
        return [
            'cogs' => 'int',
            'subtotal' => 'int',
            'discount' => 'int',
            'total' => 'int',
            'channel' => OrderChannel::class,
            'status' => OrderStatus::class,
        ];
    }

    #[Scope]
    public function paid(Builder $query): void
    {
        $query->where('status', OrderStatus::PAID->value);
    }

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

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

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

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

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

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