<?php

use App\Models\Branch;
use App\Models\Category;
use App\Models\Deal;
use App\Models\DealSlot;
use App\Models\DealSlotItem;
use App\Models\Item;
use App\Models\User;

use function Pest\Laravel\getJson;

beforeEach(function () {
    // Set up database environment manually to guarantee valid foreign constraint references
    $this->owner = User::factory()->create([
        'role' => 'owner',
    ]);

    $this->branch = Branch::create([
        'user_id' => $this->owner->id,
        'name' => 'Main Street Branch',
        'slug' => 'main-street-branch',
        'address' => '123 Main St',
        'phone' => '1234567890',
        'active' => true,
    ]);

    $this->biller = User::factory()->create([
        'role' => 'biller',
        'active_branch_id' => $this->branch->id,
        'parent_id' => $this->owner->id,
        'active' => 1,
    ]);

    $this->token = $this->biller->createToken('test-token')->plainTextToken;
});

test('biller can synchronize categories via api', function () {
    $category = Category::create([
        'branch_id' => $this->branch->id,
        'name' => 'Appetizers',
        'slug' => 'appetizers',
        'is_active' => true,
    ]);

    $subCategory = Category::create([
        'branch_id' => $this->branch->id,
        'parent_id' => $category->id,
        'name' => 'Soups',
        'slug' => 'soups',
        'is_active' => true,
    ]);

    $response = getJson(
        url('api/biller/categories'),
        ['Authorization' => "Bearer $this->token"]
    );

    $response->assertStatus(200)
        ->assertJsonFragment(['name' => 'Appetizers'])
        ->assertJsonFragment(['name' => 'Soups']);
});

test('biller can synchronize items with variants via api', function () {
    $category = Category::create([
        'branch_id' => $this->branch->id,
        'name' => 'Burgers',
        'slug' => 'burgers',
        'is_active' => true,
    ]);

    $item = Item::create([
        'branch_id' => $this->branch->id,
        'category_id' => $category->id,
        'name' => 'Chicken Burger',
        'slug' => 'chicken-burger',
        'price' => 12.50,
        'dine_in_price' => 12.50,
        'delivery_price' => 14.00,
        'takeaway_price' => 12.50,
        'gst_percentage' => 5.00,
        'tax_inclusive' => true,
        'available_dine_in' => true,
        'available_delivery' => true,
        'available_takeaway' => true,
        'is_active' => true,
    ]);

    $response = getJson(
        url('api/biller/items'),
        ['Authorization' => "Bearer $this->token"]
    );

    $response->assertStatus(200)
        ->assertJsonFragment(['name' => 'Chicken Burger'])
        ->assertJsonFragment(['name' => 'Burgers']);
});

test('biller can synchronize waiters via api', function () {
    $waiter = User::factory()->create([
        'role' => 'waiter',
        'active_branch_id' => $this->branch->id,
        'parent_id' => $this->owner->id,
        'active' => 1,
        'name' => 'Alex Carter',
    ]);

    $response = getJson(
        url('api/biller/waiters'),
        ['Authorization' => "Bearer $this->token"]
    );

    $response->assertStatus(200)
        ->assertJsonFragment(['name' => 'Alex Carter'])
        ->assertJsonFragment(['role' => 'waiter']);
});

test('biller can synchronize deals valid for today', function () {
    $currentDay = now()->format('l');

    $deal = Deal::create([
        'branch_id' => $this->branch->id,
        'name' => 'Family Feast Combo',
        'description' => 'Burger combo',
        'price' => 20.00,
        'is_active' => true,
        'available_days' => [$currentDay],
    ]);

    $slot = DealSlot::create([
        'deal_id' => $deal->id,
        'name' => 'Choose Main Course',
        'quantity' => 1,
    ]);

    $response = getJson(
        url('api/biller/deals'),
        ['Authorization' => "Bearer $this->token"]
    );

    $response->assertStatus(200)
        ->assertJsonFragment(['name' => 'Family Feast Combo'])
        ->assertJsonFragment(['name' => 'Choose Main Course']);
});

test('synchronization endpoints require authentication', function () {
    getJson(url('api/biller/categories'))->assertStatus(401);
    getJson(url('api/biller/items'))->assertStatus(401);
    getJson(url('api/biller/waiters'))->assertStatus(401);
    getJson(url('api/biller/deals'))->assertStatus(401);
});
