import { HttpClient } from '@angular/common/http';
import { Injectable, inject } from '@angular/core';
import { Observable } from 'rxjs';
import { environment } from '../../environments/environment';

export type TripStatus = 'DRAFT' | 'PLANNING' | 'BOOKING' | 'UPCOMING' | 'ACTIVE' | 'COMPLETED' | 'ARCHIVED';

export interface Trip {
  id: string;
  name: string;
  description: string | null;
  ownerId: string;
  startDate: string | null;
  endDate: string | null;
  status: TripStatus;
  planningStage: string | null;
  currency: string;
  version: number;
  createdAt: string;
  updatedAt: string;
}

export interface CreateTripDto {
  name: string;
  description?: string;
  startDate?: string;
  endDate?: string;
  currency?: string;
}

export interface UpdateTripDto {
  name?: string;
  description?: string | null;
  version: number;
}

export interface TripMember {
  id: string;
  tripId: string;
  userId: string;
  role: 'OWNER' | 'MEMBER';
  status: 'INVITED' | 'ACTIVE' | 'DECLINED';
}

@Injectable({ providedIn: 'root' })
export class TripsApiService {
  private readonly http = inject(HttpClient);
  private readonly baseUrl = `${environment.apiBaseUrl}/trips`;

  listTrips(): Observable<Trip[]> {
    return this.http.get<Trip[]>(this.baseUrl);
  }

  createTrip(dto: CreateTripDto): Observable<Trip> {
    return this.http.post<Trip>(this.baseUrl, dto);
  }

  getTrip(tripId: string): Observable<Trip> {
    return this.http.get<Trip>(`${this.baseUrl}/${tripId}`);
  }

  updateTrip(tripId: string, dto: UpdateTripDto): Observable<Trip> {
    return this.http.patch<Trip>(`${this.baseUrl}/${tripId}`, dto);
  }

  listMembers(tripId: string): Observable<TripMember[]> {
    return this.http.get<TripMember[]>(`${this.baseUrl}/${tripId}/members`);
  }
}
