<?php

namespace Tests\Feature\V3\Damage;

use App\Models\Damage;
use App\Models\Permission;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use App\Models\User;
class StoreTest extends TestCase
{
    use RefreshDatabase, WithFaker;

    public function test_unauthenticated_user_cannot_store_damage(): void
    {
        $response = $this->postJson(route('v3.damages.store'));
        $response->assertStatus(401);
    }

    public function test_user_without_permission_cannot_store_damage(): void
    {
        $user = User::factory()->create();

        $response = $this->actingAs($user)->postJson(route('v3.damages.store'));
        $response->assertForbidden();
    }

    public function test_validation_fails_when_required_fields_are_missing(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'manage-damage'
            ]))
            ->create();

        $response = $this->actingAs($user)->postJson(route('v3.damages.store'));

        $response->assertUnprocessable()
            ->assertInvalid([
                'title' => __('validation.required', ['attribute' => "عنوان"]),
                'unit' => __('validation.required', ['attribute' => 'واحد']),
                'base_price' => __('validation.required', ['attribute' => 'قیمت پایه']),
            ]);
    }

    public function test_title_must_be_unique(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'manage-damage'
            ]))
            ->create();
        $damage = Damage::factory()->create();

        $response = $this->actingAs($user)->postJson(route('v3.damages.store'), [
            'title' => $damage->title,
            'unit' => 'kg',
            'base_price' => 5000,
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'title' => __('validation.unique', ['attribute' => "عنوان"]),
            ]);
    }

    public function test_store_title_and_unit_must_be_string(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'manage-damage'
            ]))
            ->create();

        $response = $this->actingAs($user)->postJson(route('v3.damages.store'), [
            'title' => 12345,
            'unit' => 67890,
            'base_price' => 10000,
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'title' => __('validation.string', ['attribute' => "عنوان"]),
                'unit' => __('validation.string', ['attribute' => 'واحد']),
            ]);
    }

    public function test_store_title_must_not_exceed_255_characters(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'manage-damage'
            ]))
            ->create();

        $longTitle = $this->faker->text(500);

        $response = $this->actingAs($user)->postJson(route('v3.damages.store'), [
            'title' => $longTitle,
            'unit' => 'kg',
            'base_price' => 10000,
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'title' => __('validation.max.string',['attribute' => 'عنوان', 'max' => 255]),
            ]);
    }

    public function test_store_base_price_must_be_intger(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'manage-damage'
            ]))
            ->create();

        $response = $this->actingAs($user)->postJson(route('v3.damages.store'), [
            'title' => 'Valid Title',
            'unit' => 'm',
            'base_price' => 'valid intger',
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'base_price' => __('validation.integer', ['attribute' => 'قیمت پایه']),
            ]);
    }


    public function test_user_with_permission_can_store_damage(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'manage-damage'
            ]))
            ->create();

        $data = [
            'title' => 'New Damage',
            'unit' => 'm',
            'base_price' => 10000,
        ];

        $response = $this->actingAs($user)->postJson(route('v3.damages.store'), $data);

        $response->assertOk()
            ->assertJson([
                'message' => __('messages.successful'),
            ]);

        $this->assertDatabaseHas('damages', $data);
    }

}
