<?php

namespace Tests\Feature\Admin;

use Tests\TestCase;
use App\Models\System\Company;
use App\Models\System\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;

class CompanySettingsTest extends TestCase
{
    use RefreshDatabase;

    protected $admin;

    protected function setUp(): void
    {
        parent::setUp();
        
        // Setup a system admin with permissions
        $this->admin = User::factory()->create(['company_id' => null]);
        $this->admin->givePermissionTo(Permission::create(['name' => 'menu.permissions.companies']));
    }

    public function test_can_update_company_settings()
    {
        $company = Company::factory()->create();
        
        $settings = [
            'enable_material_code' => true,
            'enable_points' => true,
        ];

        $response = $this->actingAs($this->admin)
            ->put(route('admin.permission.companies.update', $company->id), [
                'name' => 'Updated Name',
                'code' => $company->code,
                'status' => 1,
                'settings' => $settings,
            ]);

        $response->assertRedirect();
        $this->assertDatabaseHas('companies', [
            'id' => $company->id,
            'name' => 'Updated Name',
        ]);
        
        $updatedCompany = Company::find($company->id);
        $this->assertEquals($settings, $updatedCompany->settings);
    }

    public function test_can_create_company_with_settings()
    {
        $settings = [
            'enable_material_code' => false,
            'enable_points' => true,
        ];

        $response = $this->actingAs($this->admin)
            ->post(route('admin.permission.companies.store'), [
                'name' => 'New Company',
                'code' => 'NEW001',
                'status' => 1,
                'settings' => $settings,
            ]);

        $response->assertRedirect();
        $company = Company::where('code', 'NEW001')->first();
        $this->assertNotNull($company);
        $this->assertEquals($settings, $company->settings);
    }

    public function test_can_reuse_code_after_deletion()
    {
        $code = 'REUSE001';
        $company = Company::factory()->create(['code' => $code]);
        
        // Delete the company
        $response = $this->actingAs($this->admin)
            ->delete(route('admin.permission.companies.destroy', $company->id));
            
        $response->assertRedirect();
        $this->assertSoftDeleted('companies', ['id' => $company->id]);
        
        // The original code should be freed (renamed in the DB)
        $this->assertDatabaseMissing('companies', [
            'id' => $company->id,
            'code' => $code,
            'deleted_at' => null
        ]);
        
        // Should be able to create a new company with the same code
        $response = $this->actingAs($this->admin)
            ->post(route('admin.permission.companies.store'), [
                'name' => 'Brand New Company',
                'code' => $code,
                'status' => 1,
            ]);
            
        $response->assertRedirect();
        $this->assertDatabaseHas('companies', [
            'name' => 'Brand New Company',
            'code' => $code,
            'deleted_at' => null
        ]);
    }
}
