<?php

namespace Tests\Feature;

use App\Models\Company;
use App\Models\Driver;
use App\Models\Package;
use App\Models\PaymentMethod;
use App\Models\Subscription;
use App\Models\Truck;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;

class WebOwnerFlowTest extends TestCase
{
    use RefreshDatabase;

    public function test_owner_can_complete_full_web_flow_with_ocr_stats_and_payment(): void
    {
        Storage::fake('public');

        $company = Company::create([
            'name' => 'Flow Freight LLC',
            'email' => 'ops@flowfreight.test',
            'default_rate_per_mile' => 2.50,
            'default_additional_charge' => 75,
        ]);

        $owner = User::factory()->create([
            'company_id' => $company->id,
            'role' => 'owner',
        ]);

        $truck = Truck::create([
            'company_id' => $company->id,
            'number' => 'FLOW-101',
            'name' => 'Flow Truck',
            'fuel_cost_per_mile' => 0.60,
        ]);

        $driver = Driver::create([
            'company_id' => $company->id,
            'truck_id' => $truck->id,
            'name' => 'Demo Driver',
            'email' => 'driver@flowfreight.test',
            'phone' => '555-0100',
            'salary_per_mile' => 0.75,
        ]);

        $package = Package::create([
            'name' => 'Flow Plan',
            'description' => 'Full flow test package',
            'driver_limit' => 5,
            'truck_limit' => 5,
            'price' => 99,
            'base_price' => 49,
            'price_per_driver' => 10,
            'billing_cycle' => 'monthly',
            'is_active' => true,
        ]);

        $paymentMethod = PaymentMethod::create([
            'name' => 'Stripe Sandbox',
            'provider' => 'stripe',
            'description' => 'Demo stripe method',
            'credentials' => ['api_key' => 'sk_test_demo_key'],
            'is_active' => true,
        ]);

        $document = UploadedFile::fake()->createWithContent('owner-flow.pdf', <<<TEXT
Load ID: FLOW-LOAD-001
Pickup Date: 04/01/2026
Delivery Date: 04/02/2026
Distance: 620 miles
Weight: 42000 lbs
Company: Flow Brokers
Commodity: General Goods
Pickup Location: Dallas, TX
Delivery Location: Houston, TX
TEXT);

        $this->actingAs($owner)
            ->get(route('documents.create'))
            ->assertOk();

        $this->actingAs($owner)
            ->post(route('documents.store'), [
                'document_file' => $document,
                'driver_id' => $driver->id,
                'rate_type' => 'per_mile',
                'rate_per_mile' => 2.50,
                'additional_charge' => 75,
            ])
            ->assertRedirect(route('documents.create'));

        $analyticsResponse = $this->actingAs($owner)
            ->get(route('owner.analytics.index'));

        $analyticsResponse->assertOk();
        $analyticsResponse->assertSee('FLOW-101');
        $analyticsResponse->assertSee('Demo Driver');
        $analyticsResponse->assertSee('1,625.00', false);
        $analyticsResponse->assertSee('788.00', false);

        $this->actingAs($owner)
            ->post(route('subscriptions.store'), [
                'package_id' => $package->id,
            ])
            ->assertRedirect();

        $subscription = Subscription::where('user_id', $owner->id)->latest()->firstOrFail();

        $this->actingAs($owner)
            ->get(route('payments.checkout', ['subscription' => $subscription->id]))
            ->assertOk();

        $paymentResponse = $this->actingAs($owner)
            ->post(route('payments.process'), [
                'subscription_id' => $subscription->id,
                'payment_method_id' => $paymentMethod->id,
            ]);

        $paymentResponse->assertRedirect();

        $subscription->refresh();
        $payment = $subscription->payments()->latest()->first();

        $this->assertNotNull($payment);
        $this->assertSame('completed', $payment->status);
        $this->assertSame('active', $subscription->status);
        $this->assertSame('completed', $subscription->payment_status);
    }
}
