import { beforeEach, describe, expect, it } from "bun:test";
import { create, type StoreApi, type UseBoundStore } from "zustand";
import { authStateCreator, type AuthState } from "../../stores/auth.store";

// Test the pure state logic via authStateCreator — no React Native imports needed.
// The persisted useAuthStore (with expo-secure-store) is tested via Expo Go.

let store: UseBoundStore<StoreApi<AuthState>>;

describe("authStore", () => {
  beforeEach(() => {
    store = create<AuthState>()(authStateCreator);
  });

  it("initial state is unauthenticated", () => {
    const state = store.getState();
    expect(state.isAuthenticated).toBe(false);
    expect(state.user).toBeNull();
    expect(state.activeHouseholdId).toBeNull();
    expect(state.households).toEqual([]);
  });

  it("setUser authenticates the user", () => {
    const user = { id: "1", name: "Test User", email: "test@example.com" };
    store.getState().setUser(user);

    const state = store.getState();
    expect(state.user).toEqual(user);
    expect(state.isAuthenticated).toBe(true);
  });

  it("clearAuth clears user, activeHouseholdId and households", () => {
    store.setState({
      user: { id: "1", name: "Test", email: "t@t.com" },
      activeHouseholdId: "household-abc",
      households: [{ id: "household-abc", name: "Test Household", role: "owner" }],
      isAuthenticated: true,
    });

    store.getState().clearAuth();

    const state = store.getState();
    expect(state.user).toBeNull();
    expect(state.activeHouseholdId).toBeNull();
    expect(state.households).toEqual([]);
    expect(state.isAuthenticated).toBe(false);
  });

  it("clearSession resets user, activeHouseholdId, households and isAuthenticated", () => {
    store.setState({
      user: { id: "1", name: "Test", email: "t@t.com" },
      activeHouseholdId: "household-abc",
      households: [{ id: "household-abc", name: "Test Household", role: "owner" }],
      isAuthenticated: true,
    });

    store.getState().clearSession();

    const state = store.getState();
    expect(state.user).toBeNull();
    expect(state.activeHouseholdId).toBeNull();
    expect(state.households).toEqual([]);
    expect(state.isAuthenticated).toBe(false);
  });

  it("setActiveHousehold stores the id", () => {
    store.getState().setActiveHousehold("household-123");
    expect(store.getState().activeHouseholdId).toBe("household-123");
  });

  it("setHouseholds stores the list", () => {
    const households = [
      { id: "hh-1", name: "Household One", role: "owner" },
      { id: "hh-2", name: "Household Two", role: "member" },
    ];
    store.getState().setHouseholds(households);
    expect(store.getState().households).toEqual(households);
  });

  it("clearAuth resets activeHouseholdId and households", () => {
    store.getState().setActiveHousehold("household-123");
    store.getState().setHouseholds([{ id: "household-123", name: "My Home", role: "owner" }]);
    store.getState().clearAuth();
    expect(store.getState().activeHouseholdId).toBeNull();
    expect(store.getState().households).toEqual([]);
  });
});
