import api from './api';

export interface LoginPayload {
  tenant_slug: string;
  username: string;
  password: string;
}

export interface LoginResponse {
  token: string;
  user: {
    id: number;
    name: string;
    username: string;
    email: string;
    role: string;
    tenant_slug: string;
  };
}

export const authService = {
  async checkTenantExists(slug: string): Promise<boolean> {
    const res = await api.get(`/api/v1/auth/tenant/${slug}/exists`);
    return res.data?.exists === true;
  },

  async login(payload: LoginPayload): Promise<LoginResponse> {
    const res = await api.post('/api/v1/auth/login', payload);
    return res.data;
  },

  async getProfile(): Promise<LoginResponse['user']> {
    const res = await api.get('/api/v1/auth/me');
    return res.data;
  },

  async changePassword(currentPassword: string, newPassword: string): Promise<void> {
    await api.post('/api/v1/auth/change-password', {
      current_password: currentPassword,
      new_password: newPassword,
    });
  },
};
