import {
  countries, cities, operators, lines,
  type Country, type InsertCountry,
  type City, type InsertCity,
  type Operator, type InsertOperator,
  type Line, type InsertLine,
} from "@shared/schema";
import { db } from "./db";
import { eq } from "drizzle-orm";

export interface IStorage {
  // Countries
  getAllCountries(): Promise<Country[]>;
  getCountry(id: string): Promise<Country | undefined>;
  createCountry(data: InsertCountry): Promise<Country>;
  deleteCountry(id: string): Promise<void>;
  
  // Cities
  getCitiesByCountry(countryId: string): Promise<City[]>;
  createCity(data: InsertCity): Promise<City>;
  deleteCity(id: string): Promise<void>;
  
  // Operators
  getOperatorsByCity(cityId: string): Promise<Operator[]>;
  createOperator(data: InsertOperator): Promise<Operator>;
  deleteOperator(id: string): Promise<void>;
  
  // Lines
  getLinesByOperator(operatorId: string): Promise<Line[]>;
  createLine(data: InsertLine): Promise<Line>;
  updateLine(id: string, data: Partial<InsertLine>): Promise<Line | undefined>;
  deleteLine(id: string): Promise<void>;
}

export class DatabaseStorage implements IStorage {
  // Countries
  async getAllCountries(): Promise<Country[]> {
    return await db.select().from(countries);
  }
  
  async getCountry(id: string): Promise<Country | undefined> {
    const [country] = await db.select().from(countries).where(eq(countries.id, id));
    return country;
  }
  
  async createCountry(data: InsertCountry): Promise<Country> {
    const [country] = await db.insert(countries).values(data).returning();
    return country;
  }
  
  async deleteCountry(id: string): Promise<void> {
    await db.delete(countries).where(eq(countries.id, id));
  }
  
  // Cities
  async getCitiesByCountry(countryId: string): Promise<City[]> {
    return await db.select().from(cities).where(eq(cities.countryId, countryId));
  }
  
  async createCity(data: InsertCity): Promise<City> {
    const [city] = await db.insert(cities).values(data).returning();
    return city;
  }
  
  async deleteCity(id: string): Promise<void> {
    await db.delete(cities).where(eq(cities.id, id));
  }
  
  // Operators
  async getOperatorsByCity(cityId: string): Promise<Operator[]> {
    return await db.select().from(operators).where(eq(operators.cityId, cityId));
  }
  
  async createOperator(data: InsertOperator): Promise<Operator> {
    const [operator] = await db.insert(operators).values(data).returning();
    return operator;
  }
  
  async deleteOperator(id: string): Promise<void> {
    await db.delete(operators).where(eq(operators.id, id));
  }
  
  // Lines
  async getLinesByOperator(operatorId: string): Promise<Line[]> {
    return await db.select().from(lines).where(eq(lines.operatorId, operatorId));
  }
  
  async createLine(data: InsertLine): Promise<Line> {
    const [line] = await db.insert(lines).values(data).returning();
    return line;
  }
  
  async updateLine(id: string, data: Partial<InsertLine>): Promise<Line | undefined> {
    const [line] = await db.update(lines).set(data).where(eq(lines.id, id)).returning();
    return line;
  }
  
  async deleteLine(id: string): Promise<void> {
    await db.delete(lines).where(eq(lines.id, id));
  }
}

export const storage = new DatabaseStorage();
