<?php

namespace Tests\Feature\Api;

class BranchManagementTest extends ApiTestCase
{
    protected function setUp(): void
    {
        parent::setUp();
        $this->createAuthenticatedUser();
    }

    public function test_can_list_all_branches(): void
    {
        $this->createBranch(['name' => 'Branch 2', 'code' => 'BR002']);
        $this->createBranch(['name' => 'Branch 3', 'code' => 'BR003']);

        $response = $this->getJson('/api/v1/branches');

        $this->assertSuccessResponse($response);
        $this->assertGreaterThanOrEqual(3, count($response->json('data')));
    }

    public function test_can_create_branch(): void
    {
        $response = $this->postJson('/api/v1/branches', [
            'name' => 'New Branch',
            'code' => 'NB001',
            'address' => '456 New Street',
            'phone' => '9876543210',
            'email' => 'newbranch@test.com',
            'is_active' => true,
        ]);

        $this->assertSuccessResponse($response, 201);
        $this->assertDatabaseHas('branches', [
            'name' => 'New Branch',
            'code' => 'NB001',
        ]);
    }

    public function test_branch_code_must_be_unique(): void
    {
        $this->createBranch(['code' => 'UNIQUE001']);

        $response = $this->postJson('/api/v1/branches', [
            'name' => 'Another Branch',
            'code' => 'UNIQUE001',
            'address' => '789 Test Street',
        ]);

        $response->assertStatus(422);
        $response->assertJsonValidationErrors(['code']);
    }

    public function test_can_show_single_branch(): void
    {
        $branch = $this->createBranch(['name' => 'Show Branch']);

        $response = $this->getJson("/api/v1/branches/{$branch->id}");

        $this->assertSuccessResponse($response);
        $response->assertJson([
            'data' => [
                'name' => 'Show Branch',
            ],
        ]);
    }

    public function test_can_update_branch(): void
    {
        $branch = $this->createBranch(['name' => 'Old Name']);

        $response = $this->putJson("/api/v1/branches/{$branch->id}", [
            'name' => 'Updated Name',
            'address' => 'Updated Address',
        ]);

        $this->assertSuccessResponse($response);
        $this->assertDatabaseHas('branches', [
            'id' => $branch->id,
            'name' => 'Updated Name',
        ]);
    }

    public function test_can_delete_branch(): void
    {
        $branch = $this->createBranch(['name' => 'Delete Me', 'code' => 'DEL001']);

        $response = $this->deleteJson("/api/v1/branches/{$branch->id}");

        $this->assertSuccessResponse($response, 200, false);
        $this->assertSoftDeleted('branches', ['id' => $branch->id]);
    }

    public function test_can_update_branch_settings(): void
    {
        $branch = $this->createBranch();

        $response = $this->putJson("/api/v1/branches/{$branch->id}/settings", [
            'settings' => [
                'stock_restriction_enabled' => false,
                'allow_negative_stock' => true,
                'custom_setting' => 'value',
            ],
        ]);

        $this->assertSuccessResponse($response);

        $branch->refresh();
        $this->assertEquals(false, $branch->settings['stock_restriction_enabled']);
        $this->assertEquals(true, $branch->settings['allow_negative_stock']);
    }

    public function test_can_create_child_branch(): void
    {
        $parentBranch = $this->createBranch(['name' => 'Parent Branch', 'code' => 'PAR001']);

        $response = $this->postJson('/api/v1/branches', [
            'name' => 'Child Branch',
            'code' => 'CHD001',
            'parent_id' => $parentBranch->id,
            'address' => '123 Child Street',
        ]);

        $this->assertSuccessResponse($response, 201);
        $this->assertDatabaseHas('branches', [
            'name' => 'Child Branch',
            'parent_id' => $parentBranch->id,
        ]);
    }
}
