<?php

declare(strict_types=1);

use classes\email;
use classes\pdf_store;

putenv('EMAIL_FAKE_MODE=1');

usesApiSuite();

it('allows department admins to resend order booking confirmations', function (): void {
    $customer = api_fixtures()->createUser([
        'display_name' => 'Resend Booking Confirmation Customer',
        'email' => 'resend-booking-confirmation@example.test',
    ]);
    $branding = api_fixtures()->createBranding([
        'name' => 'Resend Booking Confirmation Brand',
        'address' => 'Resend Booking Confirmation Address 1',
    ]);
    $department = api_fixtures()->createDepartment([
        'name' => 'Resend Booking Confirmation Department',
        'branding' => $branding['id'],
    ]);
    $booking = api_fixtures()->createOrderBooking([
        'customer_number' => $customer['customer_number'],
        'department' => $department['id'],
        'reference' => 'RESEND-CONFIRMATION',
        'reg_1' => 'RESEND1',
    ]);
    $session = api_fixtures()->createUserSession([
        'resend_booking_confirmations',
        'department_access_' . $department['id'],
    ]);

    $response = api_client()->post('/order-bookings/booking-confirmation/resend', [
        'id' => $booking['id'],
    ], $session['headers']);

    $response
        ->assertStatus(200)
        ->assertEnvelope()
        ->assertSuccess();

    expect($response->data())
        ->toBeArray()
        ->toHaveKey('message', 'Booking confirmation resent successfully.')
        ->toHaveKey('booking')
        ->and($response->data()['booking'])
        ->toBeArray()
        ->and($response->data()['booking']['id'] ?? null)
        ->toBe($booking['id'])
        ->and($response->data()['booking']['reference'] ?? null)
        ->toBe('RESEND-CONFIRMATION');
});

it('requires department access when resending order booking confirmations', function (): void {
    $customer = api_fixtures()->createUser([
        'display_name' => 'Resend Booking Confirmation Foreign Customer',
    ]);
    $department = api_fixtures()->createDepartment([
        'name' => 'Resend Booking Confirmation Foreign Department',
    ]);
    $booking = api_fixtures()->createOrderBooking([
        'customer_number' => $customer['customer_number'],
        'department' => $department['id'],
    ]);
    $session = api_fixtures()->createUserSession([
        'resend_booking_confirmations',
    ]);

    $response = api_client()->post('/order-bookings/booking-confirmation/resend', [
        'id' => $booking['id'],
    ], $session['headers']);

    $response
        ->assertStatus(403)
        ->assertEnvelope()
        ->assertSuccess(false)
        ->assertMissingPermissions(['department_access_' . $department['id']]);
});

it('allows department admins to resend order booking completion confirmations', function (): void {
    email::resetFakeDeliveries();

    $customer = api_fixtures()->createUser([
        'display_name' => 'Resend Completion Confirmation Customer',
        'email' => 'resend-completion-confirmation@example.test',
    ]);
    $department = api_fixtures()->createDepartment([
        'name' => 'Resend Completion Confirmation Department',
    ]);
    $cashier = api_fixtures()->createUser([
        'display_name' => 'Resend Completion Confirmation Cashier',
    ]);
    $order = api_fixtures()->createOrder([
        'customer_id' => $customer['customer_number'],
        'cashier_id' => $cashier['id'],
        'department_id' => $department['id'],
    ]);
    $booking = api_fixtures()->createOrderBooking([
        'customer_number' => $customer['customer_number'],
        'department' => $department['id'],
        'reference' => 'RESEND-COMPLETION',
        'reg_1' => 'DONE1',
        'order_id' => $order['id'],
    ]);
    api_fixtures()->createOrderAttachment([
        'order_id' => $order['id'],
        'content' => json_encode([
            'document' => 'completion-confirmation-test.pdf',
            'other' => 'wash_certificate',
        ], JSON_THROW_ON_ERROR),
    ]);
    (new pdf_store())->createObject('completion-confirmation-test.pdf', '%PDF-1.4 test completion confirmation');
    $session = api_fixtures()->createUserSession([
        'complete_bookings',
        'department_access_' . $department['id'],
    ]);

    $response = api_client()->post('/order-bookings/completion-confirmation/resend', [
        'id' => $booking['id'],
    ], $session['headers']);

    $response
        ->assertStatus(200)
        ->assertEnvelope()
        ->assertSuccess();

    expect($response->data())
        ->toBeArray()
        ->toHaveKey('message', 'Completion confirmation resent successfully.')
        ->toHaveKey('booking')
        ->and($response->data()['booking'])
        ->toBeArray()
        ->and($response->data()['booking']['id'] ?? null)
        ->toBe($booking['id'])
        ->and(email::$fake_deliveries)
        ->toHaveCount(1)
        ->and(email::$fake_deliveries[0]['to'] ?? null)
        ->toBe('resend-completion-confirmation@example.test')
        ->and(email::$fake_deliveries[0]['subject'] ?? '')
        ->toContain('RESEND-COMPLETION');
});

it('requires department access when resending order booking completion confirmations', function (): void {
    $customer = api_fixtures()->createUser([
        'display_name' => 'Resend Completion Confirmation Foreign Customer',
    ]);
    $department = api_fixtures()->createDepartment([
        'name' => 'Resend Completion Confirmation Foreign Department',
    ]);
    $order = api_fixtures()->createOrder([
        'customer_id' => $customer['customer_number'],
        'department_id' => $department['id'],
    ]);
    $booking = api_fixtures()->createOrderBooking([
        'customer_number' => $customer['customer_number'],
        'department' => $department['id'],
        'order_id' => $order['id'],
    ]);
    $session = api_fixtures()->createUserSession([
        'complete_bookings',
    ]);

    $response = api_client()->post('/order-bookings/completion-confirmation/resend', [
        'id' => $booking['id'],
    ], $session['headers']);

    $response
        ->assertStatus(403)
        ->assertEnvelope()
        ->assertSuccess(false)
        ->assertMissingPermissions(['department_access_' . $department['id']]);
});
