import { HttpClient } from '@angular/common/http';
import { Injectable, inject } from '@angular/core';
import { Observable } from 'rxjs';
import { Floor, Lead, RentListing, SchemePin, Shop, ShopCategory } from '../models/mall-content.model';
import { MallContentGateway } from './mall-content-gateway.interface';

/**
 * Contract: docs/backend/PHASE-10-CONTENT-MODULES-CONTRACT.md §2.
 * Only the lead-submission and settings endpoints are spelled out explicitly
 * (§ "PATCH /api/admin/v2/content/mall-settings", "POST .../rent-listings/{id}/leads");
 * the read endpoints below follow that same /api/admin/v2/content/ namespace.
 */
@Injectable({ providedIn: 'root' })
export class MallContentApiGateway implements MallContentGateway {
  private readonly http = inject(HttpClient);

  loadShops(): Observable<Shop[]> {
    return this.http.get<Shop[]>('/api/admin/v2/content/shops');
  }

  loadShopCategories(): Observable<ShopCategory[]> {
    return this.http.get<ShopCategory[]>('/api/admin/v2/content/shop-categories');
  }

  loadFloors(): Observable<Floor[]> {
    return this.http.get<Floor[]>('/api/admin/v2/content/floors');
  }

  loadSchemePins(floorId: string): Observable<SchemePin[]> {
    return this.http.get<SchemePin[]>(
      `/api/admin/v2/content/floors/${encodeURIComponent(floorId)}/pins`,
    );
  }

  loadRentListings(): Observable<RentListing[]> {
    return this.http.get<RentListing[]>('/api/admin/v2/content/rent-listings');
  }

  submitLead(lead: Omit<Lead, 'id' | 'createdAt'>): Observable<Lead> {
    const listingId = lead.rentListingId ?? '';
    return this.http.post<Lead>(
      `/api/admin/v2/content/rent-listings/${encodeURIComponent(listingId)}/leads`,
      lead,
    );
  }
}
