<?php

namespace App\Livewire\Forms\Studio\Manage;

use App\Models\Restock;
use App\Traits\Media\WithMediaHandler;
use App\Traits\Restock\WithRestockItems;
use App\Traits\Restock\WithRestockStock;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;
use Livewire\Form;

class RestockForm extends Form
{
    use WithMediaHandler, WithRestockItems, WithRestockStock;

    public ?Restock $restock = null;

    public ?string $note = null;

    public string $restock_date = '';

    public string $warehouse_id = '';

    public string $outlet_id = '';

    public array $image = [];

    public string $perfume_id = '';

    public string $quantity_perfume = '';

    public string $product_id = '';

    public string $quantity_product = '';

    public string $bottle_id = '';

    public string $quantity_bottle = '';

    public string $item_id = '';

    public string $quantity_edit = '';

    public function rules(): array
    {
        return [
            'note' => ['nullable', 'string', 'max:100'],
            'restock_date' => ['required', 'date', 'before_or_equal:today'],
            'warehouse_id' => ['required', Rule::exists('warehouses', 'id')],
            'outlet_id' => ['required', Rule::exists('outlets', 'id')],
            'image' => ['nullable', 'array', 'max:1'],
        ];
    }

    public function validationAttributes(): array
    {
        return [
            'note' => 'catatan',
            'restock_date' => 'tanggal restock',
            'warehouse_id' => 'gudang',
            'outlet_id' => 'outlet',
            'image' => 'gambar',
        ];
    }

    public function setRestock(Restock $restock): void
    {
        $this->restock = $restock;

        $this->note = $restock->note;
        $this->restock_date = $restock->restock_date;
        $this->warehouse_id = $restock->warehouse_id;
        $this->outlet_id = $restock->outlet_id;

        $this->image = $this->mapMediaCollection($restock->getMedia('image'));
    }

    public function store(): void
    {
        $this->validate();

        $restockItems = $this->loadRestockItems();

        DB::transaction(function () use ($restockItems) {
            $data = $this->prepareSavedData();

            $restock = Restock::create($data);

            $restock->load(['warehouse', 'outlet']);

            $this->uploadMedia($this->image, $restock, 'image');

            foreach ($restockItems as $item) {
                if ($item->quantity <= 0) {
                    continue;
                }

                $item->update(['restock_id' => $restock->id]);

                $this->transferStockFromWarehouseToOutlet($restock->warehouse, $restock->outlet, $item);
            }
        });
    }

    public function update(): void
    {
        $this->validate();

        DB::transaction(function () {
            $data = $this->prepareSavedData();

            $this->restock->update($data);

            $this->syncMedia($this->image, $this->restock, 'image');
            $this->uploadMedia($this->image, $this->restock, 'image');
        });
    }

    private function prepareSavedData(): array
    {
        return [
            'note' => $this->note,
            'warehouse_id' => $this->warehouse_id,
            'outlet_id' => $this->outlet_id,
            'restock_date' => $this->restock_date,
        ];
    }
}
