import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { authContainer } from "./container";
import { AUTH_SYMBOLS } from "./symbols";
import { AuthModule } from "./module";
import { MockUsersRepository } from "@/infrastructure/repositories/users.repository.mock";
import { MockAuthenticationService } from "@/infrastructure/services/authentication.service.mock";
import type { IUsersRepository } from "@/application/repositories/users.repository.interface";
import type { IAuthenticationService } from "@/application/services/authentication.service.interface";
import type { ISignInUseCase } from "@/application/use-cases/sign-in.use-case";
import type { ISignInController } from "@/interface-adapters/controllers/sign-in.controller";
import { userFactory } from "@/__factories__/user.factory";

describe("authContainer", () => {
  beforeEach(() => {
    authContainer.unbindAll();
    authContainer.load(AuthModule);
  });

  afterEach(() => {
    authContainer.unbindAll();
  });

  it("resolves IUsersRepository to MockUsersRepository by default", () => {
    const repo = authContainer.get<IUsersRepository>(
      AUTH_SYMBOLS.IUsersRepository,
    );
    expect(repo).toBeInstanceOf(MockUsersRepository);
  });

  it("resolves IAuthenticationService to MockAuthenticationService by default", () => {
    const service = authContainer.get<IAuthenticationService>(
      AUTH_SYMBOLS.IAuthenticationService,
    );
    expect(service).toBeInstanceOf(MockAuthenticationService);
  });

  it("authentication service receives users repository via constructor injection", async () => {
    const service = authContainer.get<IAuthenticationService>(
      AUTH_SYMBOLS.IAuthenticationService,
    );
    const user = userFactory.build({ id: "1", username: "alice", passwordHash: "hashed_password_alice" });
    const { session, cookie } = await service.createSession(user);
    expect(session.userId).toBe("1");
    expect(cookie.value).toBe(session.id);

    const validated = await service.validateSession(session.id);
    expect(validated.user.username).toBe("alice");
  });

  it("resolves ISignInUseCase via toDynamicValue binding", () => {
    const useCase = authContainer.get<ISignInUseCase>(AUTH_SYMBOLS.ISignInUseCase);
    expect(typeof useCase).toBe("function");
  });

  it("resolves ISignInController via toDynamicValue binding", () => {
    const controller = authContainer.get<ISignInController>(AUTH_SYMBOLS.ISignInController);
    expect(typeof controller).toBe("function");
  });
});
