<?php

namespace App\Console\Commands;

use App\Models\Customer;
use App\Models\Order;
use App\Services\SmsService;
use Illuminate\Console\Command;

class TestOrderNotificationCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'test:order-notification';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Test order status notification SMS';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $this->info('Testing order notification SMS...');

        // Get or create a test customer with your phone number
        $customer = Customer::first();
        if (!$customer) {
            $this->error('No customers found in database');
            return 1;
        }

        // Update customer phone to your number
        $customer->update(['phone' => '07043167491']);
        $this->info("Updated customer {$customer->full_name} phone to 07043167491");

        // Get or create a test order
        $order = Order::where('customer_id', $customer->id)->first();
        if (!$order) {
            $this->error('No orders found for this customer');
            return 1;
        }

        $this->info("Found order: {$order->order_number}");

        // Test SMS notification
        $smsService = new SmsService();

        $this->info('Sending order ready notification...');
        $result = $smsService->sendOrderReadyNotification(
            $customer->phone,
            $customer->full_name,
            $order->order_number
        );

        if ($result['success']) {
            $this->info('✅ Order notification sent successfully!');
            $this->info("Reference ID: " . ($result['ref_id'] ?? 'N/A'));
        } else {
            $this->error('❌ Failed to send order notification: ' . $result['message']);
        }

        return 0;
    }
}
