<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
use App\Models\GatePass;
use App\Models\Account;
use App\Models\FactoryInvoice;
use App\Models\FactoryInvoiceItem;
use App\Models\DeliveryReceipt;
use App\Models\DeliveryReceiptItem;

class DeliveryReceiptStoreTest extends TestCase
{
    use RefreshDatabase;

    public function test_delivery_receipt_store_creates_receipt_and_updates_invoice()
    {
        // Create test data
        $customer = Account::factory()->create(['account_type' => 'customer']);
        $deliveryMan = Account::factory()->create(['account_type' => 'employee']);

        // Create factory invoice with item
        $invoice = FactoryInvoice::factory()->create([
            'customer_account_id' => $customer->id,
            'status' => 'confirmed'
        ]);

        $invoiceItem = FactoryInvoiceItem::factory()->create([
            'factory_invoice_id' => $invoice->id,
            'ordered_quantity' => 10,
            'ordered_unit_price' => 100,
            'ordered_total' => 1000,
        ]);

        // Create gate pass
        $gatePass = GatePass::factory()->create([
            'factory_invoice_id' => $invoice->id,
            'customer_account_id' => $customer->id,
        ]);

        // Create gate pass item
        $gatePassItem = $gatePass->items()->create([
            'item_id' => $invoiceItem->item_account_id,
            'quantity' => 10,
        ]);

        // Test data for delivery receipt
        $deliveryData = [
            'gate_pass_id' => $gatePass->id,
            'delivered_by' => $deliveryMan->id,
            'delivery_date' => '2024-01-15',
            'items' => [
                [
                    'gate_pass_item_id' => $gatePassItem->id,
                    'factory_invoice_id' => $invoice->id,
                    'delivered_quantity' => 5,
                    'delivered_unit_price' => 100,
                ]
            ],
            'notes' => 'Test delivery',
            'status' => 'submitted',
        ];

        // Make the request
        $response = $this->post(route('delivery-receipts.store'), $deliveryData);

        // Assert redirect to index
        $response->assertRedirect(route('delivery-receipts.index'));

        // Assert delivery receipt was created
        $this->assertDatabaseHas('delivery_receipts', [
            'gate_pass_id' => $gatePass->id,
            'delivered_by' => $deliveryMan->id,
            'delivery_date' => '2024-01-15',
            'status' => 'submitted',
        ]);

        // Assert delivery receipt item was created
        $receipt = DeliveryReceipt::where('gate_pass_id', $gatePass->id)->first();
        $this->assertDatabaseHas('delivery_receipt_items', [
            'delivery_receipt_id' => $receipt->id,
            'gate_pass_item_id' => $gatePassItem->id,
            'customer_account_id' => $customer->id,
            'factory_invoice_id' => $invoice->id,
            'delivered_quantity' => 5,
            'delivered_unit_price' => 100,
            'delivered_total' => 500,
        ]);

        // Assert factory invoice item was updated
        $this->assertDatabaseHas('factory_invoice_items', [
            'id' => $invoiceItem->id,
            'delivered_quantity' => 5,
            'delivered_unit_price' => 100,
            'delivered_total' => 500,
        ]);

        // Assert factory invoice totals were recalculated
        $invoice->refresh();
        $this->assertEquals(5, $invoice->total_delivered_quantity);
        $this->assertEquals(500, $invoice->total_delivered_amount);
    }

    public function test_delivery_receipt_validation_fails_with_invalid_data()
    {
        $response = $this->post(route('delivery-receipts.store'), []);

        $response->assertRedirect();
        $response->assertSessionHasErrors(['gate_pass_id', 'delivered_by', 'delivery_date', 'items']);
    }
}