import { User } from "./types";
export type { User };

const AUTH_KEY = "job-portal-auth";
const API_URL = process.env.NEXT_PUBLIC_API_URL || "http://localhost:8521/api/v1";

interface LoginResponse {
  token: string;
  user: {
    id: string;
    name: string;
    email: string;
    roles: string[];
    status: string;
    created_at: string;
  };
}

export async function login(
  email: string,
  password: string,
  role?: "candidate" | "admin" | "company" // Deprecated argument, kept for signature compatibility if needed, but ignored
): Promise<User | null> {
  try {
    const res = await fetch(`${API_URL}/auth/login`, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({ email, password }),
    });

    if (!res.ok) {
      if (res.status === 401) {
        throw new Error("Credenciais inválidas");
      }
      throw new Error("Erro no servidor");
    }

    const data: LoginResponse = await res.json();

    // Map backend response to frontend User type
    // Note: The backend returns roles as an array of strings. The frontend expects a single 'role' or we need to adapt.
    // For now we map the first role or main role to the 'role' field.
    let userRole: "candidate" | "admin" | "company" = "candidate";
    if (data.user.roles.includes("superadmin") || data.user.roles.includes("admin") || data.user.roles.includes("ADMIN") || data.user.roles.includes("SUPERADMIN")) {
      userRole = "admin";
    } else if (data.user.roles.includes("companyAdmin") || data.user.roles.includes("recruiter")) {
      userRole = "company";
    }

    const user: User = {
      id: data.user.id,
      name: data.user.name,
      email: data.user.email,
      role: userRole,
      roles: data.user.roles, // Extend User type if needed, or just keep it here
      profileComplete: 80, // Mocked for now
    };

    if (typeof window !== "undefined") {
      localStorage.setItem(AUTH_KEY, JSON.stringify(user));
      localStorage.setItem("auth_token", data.token);
    }

    return user;
  } catch (error) {
    console.error("Login error:", error);
    throw error;
  }
}

export function logout(): void {
  if (typeof window !== "undefined") {
    localStorage.removeItem(AUTH_KEY);
    localStorage.removeItem("auth_token");
  }
}

export function getCurrentUser(): User | null {
  if (typeof window !== "undefined") {
    const stored = localStorage.getItem(AUTH_KEY);
    if (stored) {
      return JSON.parse(stored);
    }
  }
  return null;
}

export function isAdminUser(user: User | null): boolean {
  if (!user) return false;
  const roles = user.roles || [];
  return (
    user.role === "admin" ||
    roles.includes("superadmin") ||
    roles.includes("admin") ||
    roles.includes("ADMIN") ||
    roles.includes("SUPERADMIN")
  );
}

export function isAuthenticated(): boolean {
  return getCurrentUser() !== null;
}


export interface RegisterCandidateData {
  name: string;
  email: string;
  password: string;
  username: string; // identifier
  phone: string;
}

export async function registerCandidate(data: RegisterCandidateData): Promise<void> {
  console.log('[registerCandidate] Sending request:', { ...data, password: '***' });

  const res = await fetch(`${API_URL}/auth/register`, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify(data),
  });

  if (!res.ok) {
    const errorData = await res.json().catch(() => ({}));
    console.error('[registerCandidate] Error response:', res.status, errorData);
    throw new Error(errorData.message || `Erro no registro: ${res.status}`);
  }

  const responseData = await res.json().catch(() => ({}));
  console.log('[registerCandidate] Success response:', {
    ...responseData,
    token: responseData.token ? '***' : undefined
  });
}


export function getToken(): string | null {
  if (typeof window !== "undefined") {
    return localStorage.getItem("auth_token");
  }
  return null;
}

// Company Registration
export interface RegisterCompanyData {
  companyName: string;
  cnpj: string;
  email: string;
  phone: string;
}

export async function registerCompany(data: RegisterCompanyData): Promise<void> {
  console.log('[registerCompany] Sending request:', data);

  // Map frontend fields to backend DTO
  const payload = {
    name: data.companyName,
    document: data.cnpj,
    contact: data.phone,
    admin_email: data.email,
  };

  const res = await fetch(`${API_URL}/companies`, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify(payload),
  });

  if (!res.ok) {
    const errorData = await res.json().catch(() => ({}));
    console.error('[registerCompany] Error response:', res.status, errorData);
    throw new Error(errorData.message || `Erro no registro: ${res.status}`);
  }

  const responseData = await res.json().catch(() => ({}));
  console.log('[registerCompany] Success - Company created:', responseData);
}
