import { apiClient } from '@/services/api/client';
import { User, UserDTO } from '../types';
import { logger } from '@/utils/logger';

// Helper to map Backend DTO to Frontend Model
const mapUserDTO = (dto: UserDTO): User => ({
  id: dto.id,
  username: dto.username,
  email: dto.email,
  first_name: dto.first_name,
  last_name: dto.last_name,
  avatar: dto.avatar || 'https://via.placeholder.com/200',
  banner: dto.banner,
  bio: dto.bio,
  location: dto.location,
  roles: dto.role ? [dto.role] : ['USER'],
  role: dto.role || 'user',
  // Default boolean flags if missing in DTO
  is_active: true,
  is_verified: dto.is_verified ?? false,
  is_admin: false,
  is_public: true,
  status: 'online',
  created_at: dto.created_at
    ? new Date(dto.created_at).toISOString()
    : new Date().toISOString(),
  updated_at: dto.created_at
    ? new Date(dto.created_at).toISOString()
    : new Date().toISOString(),
  tier: dto.is_verified ? 'Pro' : 'Free',
  stats: { followers: 0, following: 0, tracks: 0, plays: 0 },
});

export const authService = {
  login: async (credentials: any) => {
    const response = await apiClient.post<{
      user: UserDTO;
      access_token: string;
      refresh_token: string;
    }>('/auth/login', credentials);
    // apiClient returns AxiosResponse, data is already unwrapped if success:true
    const data = response.data;
    // Handle both wrapped and unwrapped cases just in case, though apiClient handles unwrap
    // If apiClient unwrapped it, data is the payload.
    return {
      user: mapUserDTO(data.user),
      token: {
        access_token: data.access_token,
        refresh_token: data.refresh_token,
      },
    };
  },

  register: async (data: any) => {
    const response = await apiClient.post<{
      user: UserDTO;
      access_token: string;
      refresh_token: string;
    }>('/auth/register', data);
    const payload = response.data;
    return {
      user: mapUserDTO(payload.user),
      token: {
        access_token: payload.access_token,
        refresh_token: payload.refresh_token,
      },
    };
  },

  logout: async () => {
    try {
      await apiClient.post('/auth/logout');
    } catch (e) {
      logger.warn('Logout failed on server', { error: e });
    }
  },

  getCurrentUser: async () => {
    const response = await apiClient.get<{ user: UserDTO }>('/auth/me');
    return mapUserDTO(response.data.user);
  },

  checkUsername: async (username: string) => {
    // Assuming endpoint returns { available: boolean }
    const response = await apiClient.get<{ available: boolean }>(
      `/auth/check-username?username=${username}`,
    );
    return response.data;
  },

  verifyEmail: (token: string) =>
    apiClient.post('/auth/verify-email', { token }),

  resendVerification: (email: string) =>
    apiClient.post('/auth/resend-verification', { email }),
};
