import { readFileSync, writeFileSync, mkdirSync, existsSync } from 'fs';
import { join } from 'path';
import type { Artwork, ArtEvent, Service } from './types';
import { seedArtworks, seedEvents, seedServices } from './seed';

const DATA_DIR = process.env.DATA_DIR || '/data';

function ensureDir() {
  if (!existsSync(DATA_DIR)) {
    mkdirSync(DATA_DIR, { recursive: true });
  }
}

function readJSON<T>(filename: string, seedData: T[]): T[] {
  const filepath = join(DATA_DIR, filename);
  if (existsSync(filepath)) {
    try {
      return JSON.parse(readFileSync(filepath, 'utf-8'));
    } catch {
      return seedData;
    }
  }
  // Auto-seed on first read
  ensureDir();
  writeFileSync(filepath, JSON.stringify(seedData, null, 2));
  return seedData;
}

function writeJSON<T>(filename: string, data: T[]) {
  ensureDir();
  writeFileSync(join(DATA_DIR, filename), JSON.stringify(data, null, 2));
}

// Artworks
export function getArtworks(): Artwork[] {
  return readJSON<Artwork>('artworks.json', seedArtworks);
}

export function getArtwork(id: string): Artwork | undefined {
  return getArtworks().find(a => a.id === id);
}

export function saveArtworks(artworks: Artwork[]) {
  writeJSON('artworks.json', artworks);
}

export function addArtwork(artwork: Artwork) {
  const artworks = getArtworks();
  artworks.push(artwork);
  saveArtworks(artworks);
  return artwork;
}

export function updateArtwork(id: string, updates: Partial<Artwork>): Artwork | null {
  const artworks = getArtworks();
  const index = artworks.findIndex(a => a.id === id);
  if (index === -1) return null;
  artworks[index] = { ...artworks[index], ...updates, id };
  saveArtworks(artworks);
  return artworks[index];
}

export function deleteArtwork(id: string): boolean {
  const artworks = getArtworks();
  const filtered = artworks.filter(a => a.id !== id);
  if (filtered.length === artworks.length) return false;
  saveArtworks(filtered);
  return true;
}

// Events
export function getEvents(): ArtEvent[] {
  return readJSON<ArtEvent>('events.json', seedEvents);
}

export function getEvent(id: string): ArtEvent | undefined {
  return getEvents().find(e => e.id === id);
}

export function saveEvents(events: ArtEvent[]) {
  writeJSON('events.json', events);
}

export function addEvent(event: ArtEvent) {
  const events = getEvents();
  events.push(event);
  saveEvents(events);
  return event;
}

export function updateEvent(id: string, updates: Partial<ArtEvent>): ArtEvent | null {
  const events = getEvents();
  const index = events.findIndex(e => e.id === id);
  if (index === -1) return null;
  events[index] = { ...events[index], ...updates, id };
  saveEvents(events);
  return events[index];
}

export function deleteEvent(id: string): boolean {
  const events = getEvents();
  const filtered = events.filter(e => e.id !== id);
  if (filtered.length === events.length) return false;
  saveEvents(filtered);
  return true;
}

// Services
export function getServices(): Service[] {
  return readJSON<Service>('services.json', seedServices);
}

export function getService(id: string): Service | undefined {
  return getServices().find(s => s.id === id);
}

export function saveServices(services: Service[]) {
  writeJSON('services.json', services);
}

export function addService(service: Service) {
  const services = getServices();
  services.push(service);
  saveServices(services);
  return service;
}

export function updateService(id: string, updates: Partial<Service>): Service | null {
  const services = getServices();
  const index = services.findIndex(s => s.id === id);
  if (index === -1) return null;
  services[index] = { ...services[index], ...updates, id };
  saveServices(services);
  return services[index];
}

export function deleteService(id: string): boolean {
  const services = getServices();
  const filtered = services.filter(s => s.id !== id);
  if (filtered.length === services.length) return false;
  saveServices(filtered);
  return true;
}
