import PocketBase from 'pocketbase';
import type { RecordModel } from 'pocketbase';

export interface SliderCar {
  id: string;
  brand: string;
  model: string;
  year: number;
  horsepower: string;
  price: string;
  seats_count: string;
  doors_count: string;
  trunk_capacity: string;
  description: string;
  alt_text: string;
  order: number;
  is_active: boolean;
  max_passengers: number;
  image: string;
  // Дополнительные поля от PocketBase
  collectionId: string;
  collectionName: string;
  created: string;
  updated: string;
}

export interface SliderCarsResponse {
  page: number;
  perPage: number;
  totalItems: number;
  totalPages: number;
  items: SliderCar[];
}

export const sliderService = {
  async getSliderCars(pb: PocketBase): Promise<SliderCar[]> {
    try {
      const result = await pb.collection('slider_cars').getFullList({
        sort: 'order',
        filter: 'is_active=true',
        fields: '*, image'
      });
      
      return result.map((item: RecordModel) => ({
        id: item.id,
        brand: item.brand,
        model: item.model,
        year: item.year,
        horsepower: item.horsepower,
        price: item.price,
        seats_count: item.seats_count,
        doors_count: item.doors_count,
        trunk_capacity: item.trunk_capacity,
        description: item.description,
        alt_text: item.alt_text,
        order: item.order,
        is_active: item.is_active,
        max_passengers: item.max_passengers,
        image: item.image,
        collectionId: item.collectionId,
        collectionName: item.collectionName,
        created: item.created,
        updated: item.updated
      }));
    } catch (error) {
      console.error('Error fetching slider cars:', error);
      throw error;
    }
  }
};