<?php

use App\Models\Decision;
use App\Models\Event;
use App\Models\Segment;
use App\Models\User;
use Illuminate\Foundation\Testing\WithFaker;

uses(WithFaker::class);

it('validates segment_id is required for add_segment event', function () {
    $user = User::factory()->create([
        'email_verified_at' => now(),
    ]);
    $this->actingAs($user);

    $decision = Decision::factory()->create();

    // Ensure event exists (legacy schema requires non-null options)
    $addSegmentId = \DB::table('events')->insertGetId([
        'name' => 'Add Segment',
        'key' => 'add_segment',
        'description' => 'Attach a segment to contract',
        'active' => 1,
        'config' => json_encode([]),
        'options' => json_encode([]),
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    $payload = [
        'name' => $decision->name,
        'color_tag' => $decision->color_tag,
        'auto_mail' => false,
        'email_template_id' => null,
        'events' => [
            [
                'id' => $addSegmentId,
                'active' => true,
                'run_order' => 1,
                'config' => [
                    // 'segment_id' => missing on purpose
                    'deactivate_previous' => true,
                ],
            ],
        ],
    ];

    $response = $this->put(route('settings.decisions.update', ['id' => $decision->id]), $payload);

    $response->assertSessionHasErrors(['events.0.config.segment_id']);
});

it('validates archive_setting_id is required for archive_contract event', function () {
    $user = User::factory()->create([
        'email_verified_at' => now(),
    ]);
    $this->actingAs($user);

    $decision = Decision::factory()->create();

    $archiveEventId = \DB::table('events')->insertGetId([
        'name' => 'Archive Contract',
        'key' => 'archive_contract',
        'description' => 'Archive a contract by archive setting',
        'active' => 1,
        'config' => json_encode([]),
        'options' => json_encode([]),
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    $payload = [
        'name' => $decision->name,
        'color_tag' => $decision->color_tag,
        'auto_mail' => false,
        'email_template_id' => null,
        'events' => [
            [
                'id' => $archiveEventId,
                'active' => true,
                'run_order' => 1,
                'config' => [
                    // 'archive_setting_id' => missing on purpose
                    'reactivate' => false,
                ],
            ],
        ],
    ];

    $response = $this->put(route('settings.decisions.update', ['id' => $decision->id]), $payload);

    $response->assertSessionHasErrors(['events.0.config.archive_setting_id']);
});

it('accepts valid add_segment config and updates without validation errors', function () {
    $user = User::factory()->create([
        'email_verified_at' => now(),
    ]);
    $this->actingAs($user);

    $decision = Decision::factory()->create();

    $addSegmentId = \DB::table('events')->insertGetId([
        'name' => 'Add Segment',
        'key' => 'add_segment',
        'description' => 'Attach a segment to contract',
        'active' => 1,
        'config' => json_encode([]),
        'options' => json_encode([]),
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    $segment = Segment::factory()->create();

    $payload = [
        'name' => $decision->name,
        'color_tag' => $decision->color_tag,
        'auto_mail' => false,
        'email_template_id' => null,
        'events' => [
            [
                'id' => $addSegmentId,
                'active' => true,
                'run_order' => 1,
                'config' => [
                    'segment_id' => $segment->id,
                    'deactivate_previous' => true,
                ],
            ],
        ],
    ];

    $response = $this->put(route('settings.decisions.update', ['id' => $decision->id]), $payload);

    $response->assertSessionHasNoErrors();
    $response->assertRedirect(route('settings.workflow'));
});
