<?php

namespace App\Filament\Resources\ResellerOrders\Pages;

use App\Filament\Resources\ResellerOrders\ResellerOrderResource;
use Filament\Resources\Pages\CreateRecord;

class CreateResellerOrder extends CreateRecord
{
    protected static string $resource = ResellerOrderResource::class;

    protected function afterCreate(): void
    {
        $order = $this->record;

        // Log order creation in history
        $order->histories()->create([
            'user_id'     => auth()->id(),
            'action'      => 'order_created',
            'from_status' => null,
            'to_status'   => $order->status,
            'notes'       => 'Order created manually by administrator.',
        ]);

        try {
            \App\Services\NotificationDispatcher::dispatchOrderPlaced($order);
        } catch (\Exception $e) {
            \Illuminate\Support\Facades\Log::error('Manual order notification dispatch failed: ' . $e->getMessage());
        }
    }
}
