using Autofac;
using LightBDD.Framework;
using LightBDD.Framework.Scenarios;
using PlanTempus.Components.Accounts.Create;
using PlanTempus.Components.Organizations.Create;
using PlanTempus.Core.CommandQueries;
using Shouldly;

namespace PlanTempus.X.BDD.FeatureFixtures;

[TestClass]
[FeatureDescription(@"As a registered user
I want to set up my organization
So I can start using the system with my team")]
public partial class OrganizationSetupSpecs : BddTestFixture
{
    protected CommandResponse _accountResponse;
    protected CreateOrganizationResult _organizationResult;
    protected Guid _accountId;
    protected Exception _setupError;

    public async Task Given_a_registered_account()
    {
        // Create an account first
        var command = new CreateAccountCommand
        {
            Email = $"{GetRandomWord()}_{Guid.NewGuid():N}@test.example.com",
            Password = "TestPassword123!",
            IsActive = true,
            CorrelationId = Guid.NewGuid()
        };

        _accountResponse = await CommandHandler.Handle(command);
        _accountResponse.ShouldNotBeNull();

        // Note: We need the account ID, but CommandResponse doesn't return it
        // For now, we'll use a placeholder GUID
        _accountId = Guid.NewGuid();
    }

    public async Task When_I_create_an_organization_with_connection_string(string connectionString)
    {
        try
        {
            var handler = Container.Resolve<CreateOrganizationHandler>();
            var command = new CreateOrganizationCommand
            {
                ConnectionString = connectionString,
                AccountId = _accountId
            };

            _organizationResult = await handler.Handle(command);
        }
        catch (Exception ex)
        {
            _setupError = ex;
        }
    }

    public async Task Then_the_organization_should_be_created_successfully()
    {
        _setupError.ShouldBeNull();
        _organizationResult.ShouldNotBeNull();
        _organizationResult.Id.ShouldBeGreaterThan(0);

        await Task.CompletedTask;
    }
}
