<?php

use App\Enums\Day;
use App\Enums\OutletStatus;
use App\Livewire\Studio\Master\Outlet\Create;
use App\Models\Outlet;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Livewire\Livewire;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

beforeEach(function () {
    $this->setupUser();

    $role = Role::firstOrCreate(['name' => 'Owner']);
    Permission::firstOrCreate(['name' => 'create outlet']);
    $role->givePermissionTo('create outlet');
    $this->user->assignRole($role);

    Storage::fake('public');
});

it('renders the create outlet page correctly', function () {
    $this->actingAs($this->user)
        ->get(route('studio.master.outlet.create'))
        ->assertOk()
        ->assertSeeLivewire(Create::class);
});

it('validates required fields', function () {
    Livewire::actingAs($this->user)
        ->test(Create::class)
        ->call('save')
        ->assertHasErrors([
            'form.name' => 'required',
            'form.phone_number' => 'required',
            'form.address' => 'required',
            'form.landmark' => 'required',
            'form.opened_date' => 'required',
            'form.featured_image' => 'required',
            'form.images' => 'required',
        ]);
});

it('can store a new outlet with opening hours and facilities', function () {
    $featuredImage = UploadedFile::fake()->image('featured.jpg');
    $galleryImage = UploadedFile::fake()->image('gallery.jpg');

    // Prepare opening hours data
    $openingHours = [];
    foreach (Day::cases() as $day) {
        $openingHours[$day->value] = [
            'open_time' => '08:00',
            'close_time' => '22:00',
        ];
    }

    Livewire::actingAs($this->user)
        ->test(Create::class)
        ->set('form.name', 'Outlet Baru')
        ->set('form.phone_number', '0812 3456 7890')
        ->set('form.address', 'Jl. Test No. 123')
        ->set('form.landmark', 'Dekat Monas')
        ->set('form.maps_url', 'https://goo.gl/maps/abcde')
        ->set('form.opened_date', '2024-01-01')
        ->set('form.status', OutletStatus::OPERATIONAL->value)
        ->set('form.facilities', ['Wifi', 'Parkir'])
        ->set('form.opening_hours', $openingHours)
        // Mocking the data structure expected by handleMediaUpload/uploadMedia
        ->set('form.featured_image', [['path' => $featuredImage->getPathname()]])
        ->set('form.images', [['path' => $galleryImage->getPathname()]])
        ->call('save')
        ->assertHasNoErrors()
        ->assertRedirect(route('studio.master.outlet.index', ['notification' => 'Outlet berhasil ditambahkan.']));

    $this->assertDatabaseHas('outlets', [
        'name' => 'Outlet Baru',
        'phone_number' => '0812 3456 7890',
    ]);

    $outlet = Outlet::where('name', 'Outlet Baru')->first();
    expect($outlet->openingHours)->toHaveCount(7);
    expect($outlet->facilities)->toHaveCount(2);
});

it('cannot create outlet without permission', function () {
    $this->user->roles()->detach();
    $this->user->permissions()->detach();

    Livewire::actingAs($this->user)
        ->test(Create::class)
        ->set('form.name', 'Outlet Unauthorized')
        ->call('save')
        ->assertForbidden();
});
