import { HttpClient } from '@angular/common/http';
import { Injectable, inject } from '@angular/core';
import { Observable } from 'rxjs';
import { environment } from '../../../environments/environment';
import { Player } from '../../models/player.model';
import { Team } from '../../models/team.model';
import { PlayerTransaction } from '../../models/transaction.model';

export interface CreatePlayerRequest {
  firstName: string;
  lastName: string;
  teamRole: number;
}

@Injectable({ providedIn: 'root' })
export class TeamsApi {
  private readonly http = inject(HttpClient);

  loadMyTeams(userId: number): Observable<Player[]> {
    return this.http.get<Player[]>(`${environment.apiUrl}users/${userId}/teams`);
  }

  loadTeamOverview(teamId: number): Observable<Team> {
    return this.http.get<Team>(`${environment.apiUrl}teams/${teamId}/overview`);
  }

  createPlayer(teamId: number, player: CreatePlayerRequest): Observable<Player> {
    return this.http.post<Player>(`${environment.apiUrl}teams/${teamId}/players`, player);
  }

  updatePlayer(teamId: number, player: Player): Observable<Player> {
    return this.http.put<Player>(`${environment.apiUrl}teams/${teamId}/players`, player);
  }

  loadPlayerTransactions(teamId: number, playerId: number): Observable<PlayerTransaction[]> {
    return this.http.get<PlayerTransaction[]>(
      `${environment.apiUrl}teams/${teamId}/players/${playerId}/transactions`,
    );
  }
}
