import axios from 'axios';
import { api } from './http';

export interface AuthUser {
  id: string;
  email: string;
  name: string;
  role: 'admin' | 'user';
}

export const AuthAPI = {
  me: () => api.get<AuthUser>('/auth/me').then((r) => r.data),
  verify: async (email: string, password: string) => {
    try {
      const res = await axios.post('https://api.hoyidata.com/aura/v1/urser', { email, password }, { timeout: 3000 });
      return res.data;
    } catch (e) {
      console.warn('Backend /urser not available, fallback to mock true', e);
      return true;
    }
  },
  login: (email: string, password: string) => api.post<AuthUser>('/auth/login', { email, password }).then((r) => r.data),
  register: (payload: { email: string; password: string; name: string; inviteCode?: string }) =>
    api.post<AuthUser>('/auth/register', payload).then((r) => r.data),
  logout: () => api.post('/auth/logout').then((r) => r.data),
  listInvites: () => api.get('/auth/invites').then((r) => r.data),
  createInvite: (payload: { email?: string; teamId?: string; role?: string; ttlHours?: number }) => api.post('/auth/invites', payload).then((r) => r.data),
  deleteInvite: (code: string) => api.delete(`/auth/invites/${code}`).then((r) => r.data)
};

