<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class ScoopSale extends Model
{
    use HasFactory;

    protected $fillable = [
        'production_batch_id',
        'order_id',
        'order_item_id',
        'product_id',
        'branch_id',
        'scoop_count',
        'sale_price',
        'sold_at',
    ];

    protected $casts = [
        'scoop_count' => 'integer',
        'sale_price' => 'decimal:2',
        'sold_at' => 'datetime',
    ];

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

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

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

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

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

    public function scopeForBranch($query, int $branchId)
    {
        return $query->where('branch_id', $branchId);
    }

    public function scopeForProduct($query, int $productId)
    {
        return $query->where('product_id', $productId);
    }

    public function scopeForDate($query, string $date)
    {
        return $query->whereDate('sold_at', $date);
    }

    public function scopeBetweenDates($query, string $startDate, string $endDate)
    {
        return $query->whereBetween('sold_at', [$startDate, $endDate]);
    }
}
