/**
 * Tipos para o módulo de Usuários
 */

export type UserRole =
  | "admin"
  | "gestor"
  | "medico"
  | "secretaria"
  | "user"
  | "paciente";

export interface UserRoleRecord {
  id: string;
  user_id: string;
  role: UserRole;
  created_at: string;
}

export interface User {
  id: string;
  email: string;
  email_confirmed_at: string | null;
  created_at: string;
  last_sign_in_at: string | null;
}

export interface UserProfile {
  id: string;
  full_name: string | null;
  email: string | null;
  phone: string | null;
  avatar_url: string | null;
  disabled: boolean;
  created_at: string;
  updated_at: string;
}

export interface UserPermissions {
  isAdmin: boolean;
  isManager: boolean;
  isDoctor: boolean;
  isSecretary: boolean;
  isAdminOrManager: boolean;
}

export interface UserInfo {
  user: User;
  profile: UserProfile | null;
  roles: UserRole[];
  permissions: UserPermissions;
}

export interface CreateUserInput {
  email: string;
  full_name: string;
  phone?: string | null;
  password?: string; // Senha para registro direto (opcional - se não fornecida, envia magic link)
  role: UserRole; // Agora é obrigatório
  create_patient_record?: boolean; // Novo campo opcional
  cpf?: string; // Obrigatório se create_patient_record=true
  phone_mobile?: string; // Obrigatório se create_patient_record=true
  redirect_url?: string; // URL para redirecionar após clicar no magic link
}

export interface CreateUserResponse {
  success: boolean;
  message: string;
  user: {
    id: string;
    email: string;
    full_name: string;
    phone: string | null;
    roles: UserRole[];
  };
}

// Novos tipos para endpoints especializados (21/10/2025)

export interface CreateDoctorInput {
  email: string;
  full_name: string;
  cpf: string; // Padrão: ^\d{11}$
  crm: string;
  crm_uf: string; // Padrão: ^[A-Z]{2}$
  specialty?: string;
  phone_mobile?: string;
  redirect_url?: string; // URL para redirecionar após clicar no magic link
  // password foi REMOVIDO da API
}

export interface CreatePatientInput {
  full_name: string;
  cpf: string; // Padrão: ^\d{11}$
  email: string;
  phone_mobile: string;
  birth_date?: string; // <date> formato
  created_by: string; // <uuid> obrigatório
  // password foi REMOVIDO - agora usa create-user com create_patient_record
}

export interface CreateDoctorResponse {
  success: boolean;
  message: string;
  doctor: {
    id: string;
    user_id: string;
    email: string;
    full_name: string;
    cpf: string;
    crm: string;
    crm_uf: string;
    specialty: string;
  };
}

export interface CreatePatientResponse {
  success: boolean;
  message: string;
  patient: {
    id: string;
    user_id: string;
    email: string;
    full_name: string;
    cpf: string;
    phone_mobile: string;
  };
}
