<?php

namespace App\Domain\Inventory\Models;

use App\Domain\Inventory\Enums\StockCountStatus;
use App\Domain\Shared\Traits\Auditable;
use App\Domain\Shared\Traits\BelongsToAcademy;
use App\Domain\Shared\Traits\HasUuid;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Domain\Shared\Traits\BelongsToBranch;

class StockCount extends Model
{
    use BelongsToAcademy, HasUuid, SoftDeletes, Auditable, BelongsToBranch;

    protected $fillable = [
        'academy_id', 'branch_id',
        'warehouse_id',
        'count_number',
        'status',
        'started_at',
        'completed_at',
        'notes',
        'total_items',
        'total_discrepancies',
        'total_variance_value',
        'created_by',
        'completed_by',
        'metadata',
    ];

    protected function casts(): array
    {
        return [
            'status' => StockCountStatus::class,
            'started_at' => 'datetime',
            'completed_at' => 'datetime',
            'total_items' => 'integer',
            'total_discrepancies' => 'integer',
            'total_variance_value' => 'integer',
            'metadata' => 'array',
        ];
    }

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

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

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

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