<?php

namespace App\Filament\Resources\CashMovements\Pages;

use App\Filament\Resources\CashMovements\CashMovementResource;
use Filament\Resources\Pages\CreateRecord;

class CreateCashMovement extends CreateRecord
{
    protected static string $resource = CashMovementResource::class;

    public static function getNavigationLabel(): string
    {
        return __('filament/admin/cash_movement_resource.pages.create.navigation_label');
    }
    public function getTitle(): string
    {
        return __('filament/admin/create_cash_movement.title');
    }

    protected function mutateFormDataBeforeCreate(array $data): array
    {
        $data['user_id'] = auth()->id();
        return $data;
    }

    public function mount(): void
    {
        parent::mount();

        // Pre-fill cashbox with first active cashbox
        $firstCashbox = \App\Models\Cashbox::where('is_active', true)
            ->orderBy('id')
            ->first();

        $formData = [
            'date' => now(),
        ];

        if ($firstCashbox) {
            $formData['cashbox_id'] = $firstCashbox->id;
        }

        // Pre-fill movement type if provided in URL
        $type = request()->query('type');
        if (in_array($type, ['in', 'out'])) {
            $formData['movement_type'] = $type;
        }

        $this->form->fill($formData);
    }

}
