<?php

namespace Tests\Feature\Api;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class OnboardApiTest extends TestCase
{
    use RefreshDatabase;

    public function test_mobile_onboard_accepts_updated_payload_and_creates_records(): void
    {
        $payload = [
            'company' => [
                'name' => 'Mobile Freight LLC',
                'email' => 'company-mobile@test.com',
                'phone' => '5551234567',
            ],
            'user' => [
                'name' => 'Mobile Owner',
                'email' => 'owner-mobile@test.com',
                'password' => 'password123',
            ],
            'plan' => 'starter',
            'num_trucks' => 2,
            'fleet' => [
                [
                    'number' => 'MB-001',
                    'name' => 'Mobile Truck 1',
                    'equipment_number' => 'EQ-MB-1',
                    'fuel_cost_per_mile' => 0.72,
                ],
            ],
            'drivers' => [
                [
                    'name' => 'Driver Mobile',
                    'email' => 'driver-mobile@test.com',
                    'phone' => '5551119999',
                ],
            ],
        ];

        $response = $this->postJson('/api/onboard', $payload);

        $response->assertStatus(201)
            ->assertJsonStructure([
                'message',
                'company_id',
                'user_id',
                'plan',
                'num_trucks',
                'num_drivers',
                'total_price',
            ]);

        $companyId = $response->json('company_id');

        $this->assertDatabaseHas('companies', [
            'id' => $companyId,
            'name' => 'Mobile Freight LLC',
            'plan' => 'starter',
            'num_trucks' => 2,
        ]);

        $this->assertDatabaseHas('users', [
            'id' => $response->json('user_id'),
            'email' => 'owner-mobile@test.com',
            'company_id' => $companyId,
            'role' => 'owner',
        ]);

        $this->assertDatabaseHas('trucks', [
            'company_id' => $companyId,
            'number' => 'MB-001',
            'name' => 'Mobile Truck 1',
        ]);

        $this->assertDatabaseHas('drivers', [
            'company_id' => $companyId,
            'name' => 'Driver Mobile',
            'email' => 'driver-mobile@test.com',
        ]);
    }

    public function test_mobile_onboard_rejects_old_fleet_payload_shape(): void
    {
        $payload = [
            'company' => [
                'name' => 'Legacy Payload Inc',
                'email' => 'legacy-company@test.com',
            ],
            'user' => [
                'name' => 'Legacy Owner',
                'email' => 'legacy-owner@test.com',
                'password' => 'password123',
            ],
            'plan' => 'starter',
            'num_trucks' => 1,
            'fleet' => [
                [
                    'make' => 'Volvo',
                    'model' => 'VNL',
                    'year' => 2021,
                ],
            ],
        ];

        $this->postJson('/api/onboard', $payload)
            ->assertStatus(422)
            ->assertJsonValidationErrors(['fleet.0.number', 'fleet.0.name']);
    }
}
