import BaseService from './BaseService.js';
import TaxiMap from '../models/taxi/taxiMap.js';
import TaxiMapType from '../models/taxi/taxiMapType.js';

class TaxiMapService extends BaseService {
  constructor() {
    super();
  }

  /**
   * Holt alle verfügbaren Map-Typen
   */
  async getMapTypes() {
    try {
      const mapTypes = await TaxiMapType.findAll({
        where: { isActive: true },
        order: [['name', 'ASC']]
      });
      return mapTypes;
    } catch (error) {
      console.error('Error getting map types:', error);
      throw error;
    }
  }

  /**
   * Holt alle verfügbaren Maps
   */
  async getMaps() {
    try {
      const maps = await TaxiMap.findAll({
        where: { isActive: true },
        include: [{
          model: TaxiMapType,
          as: 'mapType'
        }],
        order: [['name', 'ASC']]
      });
      return maps;
    } catch (error) {
      console.error('Error getting maps:', error);
      throw error;
    }
  }

  /**
   * Holt eine spezifische Map
   */
  async getMapById(mapId) {
    try {
      const map = await TaxiMap.findOne({
        where: { 
          id: mapId,
          isActive: true 
        },
        include: [{
          model: TaxiMapType,
          as: 'mapType'
        }]
      });
      return map;
    } catch (error) {
      console.error('Error getting map by ID:', error);
      throw error;
    }
  }


  /**
   * Holt die Standard-Map
   */
  async getDefaultMap() {
    try {
      const map = await TaxiMap.findOne({
        where: { 
          isDefault: true,
          isActive: true 
        },
        include: [{
          model: TaxiMapType,
          as: 'mapType'
        }]
      });
      return map;
    } catch (error) {
      console.error('Error getting default map:', error);
      throw error;
    }
  }

  /**
   * Erstellt eine neue Map
   */
  async createMap(mapData) {
    try {
      const map = await TaxiMap.create(mapData);
      return map;
    } catch (error) {
      console.error('Error creating map:', error);
      throw error;
    }
  }

  /**
   * Aktualisiert eine Map
   */
  async updateMap(mapId, updateData) {
    try {
      const [updatedRowsCount] = await TaxiMap.update(updateData, {
        where: { id: mapId }
      });
      
      if (updatedRowsCount === 0) {
        throw new Error('Map not found');
      }
      
      return await this.getMapById(mapId);
    } catch (error) {
      console.error('Error updating map:', error);
      throw error;
    }
  }

  /**
   * Löscht eine Map (soft delete)
   */
  async deleteMap(mapId) {
    try {
      const [updatedRowsCount] = await TaxiMap.update(
        { isActive: false },
        { where: { id: mapId } }
      );
      
      if (updatedRowsCount === 0) {
        throw new Error('Map not found');
      }
      
      return { success: true };
    } catch (error) {
      console.error('Error deleting map:', error);
      throw error;
    }
  }

  /**
   * Setzt eine Map als Standard
   */
  async setDefaultMap(mapId) {
    try {
      // Entferne Standard-Status von allen anderen Maps
      await TaxiMap.update(
        { isDefault: false },
        { where: { isDefault: true } }
      );
      
      // Setze neue Standard-Map
      const [updatedRowsCount] = await TaxiMap.update(
        { isDefault: true },
        { where: { id: mapId } }
      );
      
      if (updatedRowsCount === 0) {
        throw new Error('Map not found');
      }
      
      return { success: true };
    } catch (error) {
      console.error('Error setting default map:', error);
      throw error;
    }
  }

  /**
   * Initialisiert Standard-Map-Typen
   */
  async initializeMapTypes() {
    try {
      const mapTypes = [
        { name: 'Corner Bottom Left', tileType: 'cornerBottomLeft', description: 'Bottom left corner tile' },
        { name: 'Corner Bottom Right', tileType: 'cornerBottomRight', description: 'Bottom right corner tile' },
        { name: 'Corner Top Left', tileType: 'cornerTopLeft', description: 'Top left corner tile' },
        { name: 'Corner Top Right', tileType: 'cornerTopRight', description: 'Top right corner tile' },
        { name: 'Horizontal', tileType: 'horizontal', description: 'Horizontal road tile' },
        { name: 'Vertical', tileType: 'vertical', description: 'Vertical road tile' },
        { name: 'Cross', tileType: 'cross', description: 'Cross intersection tile' },
        { name: 'Fuel Horizontal', tileType: 'fuelHorizontal', description: 'Horizontal road with fuel station' },
        { name: 'Fuel Vertical', tileType: 'fuelVertical', description: 'Vertical road with fuel station' },
        { name: 'T-Left', tileType: 'tLeft', description: 'T-junction facing left' },
        { name: 'T-Right', tileType: 'tRight', description: 'T-junction facing right' },
        { name: 'T-Up', tileType: 'tUp', description: 'T-junction facing up' },
        { name: 'T-Down', tileType: 'tDown', description: 'T-junction facing down' }
      ];

      for (const mapType of mapTypes) {
        await TaxiMapType.findOrCreate({
          where: { name: mapType.name },
          defaults: mapType
        });
      }

      console.log('Taxi map types initialized');
    } catch (error) {
      console.error('Error initializing map types:', error);
      throw error;
    }
  }

  /**
   * Erstellt eine Standard-Map
   */
  async createDefaultMap() {
  }
}

export default TaxiMapService;
