/**
 * @copyright 2026 NHCarrigan
 * @license Naomi's Public License
 * @author Naomi Carrigan
 */

import { Injectable, inject } from '@angular/core';
import { Observable } from 'rxjs';
import { ApiService } from './api.service';
import { User, PrimaryBadge } from '@library/shared-types';

export interface UserProfileResponse {
  id: string;
  username: string;
  displayName?: string;
  avatar?: string;
  bio?: string;
  slug?: string;
  primaryBadge?: PrimaryBadge;
  website?: string;
  discordServer?: string;
  bluesky?: string;
  github?: string;
  linkedin?: string;
  twitch?: string;
  youtube?: string;
  achievementPoints: number;
  badges: {
    isStaff: boolean;
    isMod: boolean;
    isVip: boolean;
    inDiscord: boolean;
  };
  stats: {
    suggestionsCount: number;
    suggestionsAcceptedCount: number;
    likesCount: number;
    commentsCount: number;
  };
  createdAt: Date;
}

export interface UpdateUserSettingsRequest {
  slug?: string;
  displayName?: string;
  bio?: string;
  profilePublic?: boolean;
  primaryBadge?: PrimaryBadge;
  website?: string;
  discordServer?: string;
  bluesky?: string;
  github?: string;
  linkedin?: string;
  twitch?: string;
  youtube?: string;
}

@Injectable({
  providedIn: 'root'
})
export class UserService {
  private api = inject(ApiService);

  getAllUsers(): Observable<User[]> {
    return this.api.get<User[]>('/users');
  }

  banUser(userId: string): Observable<User> {
    return this.api.post<User>(`/users/${userId}/ban`, {});
  }

  unbanUser(userId: string): Observable<User> {
    return this.api.post<User>(`/users/${userId}/unban`, {});
  }

  getMe(): Observable<User> {
    return this.api.get<User>('/users/me');
  }

  updateSettings(settings: UpdateUserSettingsRequest): Observable<User> {
    return this.api.put<User>('/users/me', settings);
  }

  getProfile(identifier: string): Observable<UserProfileResponse> {
    return this.api.get<UserProfileResponse>(`/users/profile/${identifier}`);
  }

  makeProfilePrivate(userId: string): Observable<User> {
    return this.api.post<User>(`/users/${userId}/make-private`, {});
  }

  adminUpdateUser(userId: string, settings: UpdateUserSettingsRequest): Observable<User> {
    return this.api.put<User>(`/users/${userId}`, settings);
  }
}
