<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\Qc;
use App\Models\Stock;
use App\Models\StockLedger;
use App\Models\GatePass;
use App\Models\Contact;
use App\Models\Item;
use App\Models\Warehouse;
use App\Models\WarehouseBin;
use App\Services\QcService;

class TestQcImplementation extends Command
{
    protected $signature = 'test:qc-implementation';
    protected $description = 'Test QC implementation with Stock and StockLedger models';

    public function handle()
    {
        $this->info('🧪 Testing QC Implementation...');
        $this->newLine();

        // Test 1: Check if models can be instantiated
        $this->info('1. Testing model instantiation...');
        try {
            new Qc();
            new Stock();
            new StockLedger();
            $this->info('  ✓ All models instantiated successfully');
        } catch (\Exception $e) {
            $this->error('  ✗ Model instantiation failed: ' . $e->getMessage());
            return 1;
        }

        // Test 2: Check if tables exist
        $this->info('2. Testing database tables...');
        try {
            $qcCount = Qc::count();
            $stockCount = Stock::count();
            $stockLedgerCount = StockLedger::count();
            $this->info("  ✓ Tables exist - QC: {$qcCount}, Stock: {$stockCount}, StockLedger: {$stockLedgerCount}");
        } catch (\Exception $e) {
            $this->error('  ✗ Database table check failed: ' . $e->getMessage());
            return 1;
        }

        // Test 3: Check if required data exists
        $this->info('3. Checking required data...');
        $igpCount = GatePass::where('type', 'inbound')->count();
        $contactCount = Contact::where('type', 'employee')->count();
        $itemCount = Item::count();
        $warehouseCount = Warehouse::count();
        $binCount = WarehouseBin::count();

        $this->info("  ✓ IGP Count: {$igpCount}");
        $this->info("  ✓ Employee Contacts: {$contactCount}");
        $this->info("  ✓ Items: {$itemCount}");
        $this->info("  ✓ Warehouses: {$warehouseCount}");
        $this->info("  ✓ Bins: {$binCount}");

        // Test 4: Test QcService
        $this->info('4. Testing QcService...');
        try {
            $warehouseService = app(\App\Services\WarehouseService::class);
            $numberService = app(\App\Services\NumberGenerationService::class);
            $qcService = new QcService($warehouseService, $numberService);
            $this->info('  ✓ QcService instantiated successfully');
        } catch (\Exception $e) {
            $this->error('  ✗ QcService instantiation failed: ' . $e->getMessage());
            return 1;
        }

        $this->newLine();
        $this->info('✅ QC Implementation test completed successfully!');
        $this->info('The system is ready for QC operations.');

        return 0;
    }
}
