import React from "react";
import { render, fireEvent } from "@testing-library/react-native";
import LoginScreen from "../../../src/features/auth/LoginScreen";

const baseProps = {
  appDisplayName: "MissioFlow",
  brandColor: "#1E56A8",
  logoUrl: null as string | null,
  configuredBaseUrl: "https://api.test",
  inviteToken: null as string | null,
  email: "",
  password: "",
  submitting: false,
  error: null as string | null,
  statusMessage: null as string | null,
  onChangeEmail: jest.fn(),
  onChangePassword: jest.fn(),
  onLogin: jest.fn(),
  onChangeInstance: jest.fn(),
};

describe("LoginScreen", () => {
  it("affiche titre et instance", () => {
    const { getByText } = render(<LoginScreen {...baseProps} />);
    expect(getByText("MissioFlow")).toBeTruthy();
    expect(getByText("https://api.test")).toBeTruthy();
    expect(getByText("Se connecter")).toBeTruthy();
  });

  it("affiche le logo quand logoUrl fourni", () => {
    const { UNSAFE_getAllByType } = render(
      <LoginScreen {...baseProps} logoUrl="https://a.b/logo.png" />
    );
    const { Image } = require("react-native");
    expect(UNSAFE_getAllByType(Image).length).toBeGreaterThan(0);
  });

  it("pas de logo quand logoUrl=null", () => {
    const { UNSAFE_queryAllByType } = render(<LoginScreen {...baseProps} />);
    const { Image } = require("react-native");
    expect(UNSAFE_queryAllByType(Image)).toHaveLength(0);
  });

  it("affiche inviteToken quand present", () => {
    const { getByText } = render(<LoginScreen {...baseProps} inviteToken="TOK-123" />);
    expect(getByText(/TOK-123/)).toBeTruthy();
  });

  it("affiche error quand present", () => {
    const { getByText } = render(<LoginScreen {...baseProps} error="Identifiants invalides" />);
    expect(getByText("Identifiants invalides")).toBeTruthy();
  });

  it("affiche statusMessage quand present", () => {
    const { getByText } = render(<LoginScreen {...baseProps} statusMessage="Connecte" />);
    expect(getByText("Connecte")).toBeTruthy();
  });

  it("submitting=true affiche 'Connexion...' et disable le bouton", () => {
    const { getByText } = render(<LoginScreen {...baseProps} submitting={true} />);
    expect(getByText("Connexion...")).toBeTruthy();
  });

  it("onChangeText email propage", () => {
    const onChangeEmail = jest.fn();
    const { getByPlaceholderText } = render(
      <LoginScreen {...baseProps} onChangeEmail={onChangeEmail} />
    );
    fireEvent.changeText(getByPlaceholderText("Email"), "a@b.c");
    expect(onChangeEmail).toHaveBeenCalledWith("a@b.c");
  });

  it("onChangeText password propage", () => {
    const onChangePassword = jest.fn();
    const { getByPlaceholderText } = render(
      <LoginScreen {...baseProps} onChangePassword={onChangePassword} />
    );
    fireEvent.changeText(getByPlaceholderText("Mot de passe"), "pw");
    expect(onChangePassword).toHaveBeenCalledWith("pw");
  });

  it("Se connecter appelle onLogin", () => {
    const onLogin = jest.fn();
    const { getByText } = render(<LoginScreen {...baseProps} onLogin={onLogin} />);
    fireEvent.press(getByText("Se connecter"));
    expect(onLogin).toHaveBeenCalled();
  });

  it("Changer d'instance appelle onChangeInstance", () => {
    const onChangeInstance = jest.fn();
    const { getByText } = render(
      <LoginScreen {...baseProps} onChangeInstance={onChangeInstance} />
    );
    fireEvent.press(getByText("Changer d'instance"));
    expect(onChangeInstance).toHaveBeenCalled();
  });

  it("toggle showPassword : Afficher -> Masquer et secureTextEntry bascule", () => {
    const { getByText, getByPlaceholderText } = render(<LoginScreen {...baseProps} />);
    const pwInput = getByPlaceholderText("Mot de passe");
    expect(pwInput.props.secureTextEntry).toBe(true);
    fireEvent.press(getByText("Afficher"));
    expect(getByText("Masquer")).toBeTruthy();
    expect(getByPlaceholderText("Mot de passe").props.secureTextEntry).toBe(false);
    fireEvent.press(getByText("Masquer"));
    expect(getByText("Afficher")).toBeTruthy();
  });
});
