const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database('./database/ashtransfer.db');

console.log('🌱 Seeding CMS data...\n');

// 1. Seed UK Airports
const airports = [
  { code: 'MAN', name: 'Manchester Airport', postcode: 'M90 1QX', address: 'Manchester Airport, Manchester' },
  { code: 'LHR', name: 'London Heathrow Airport', postcode: 'TW6 1EW', address: 'Longford, Hounslow, London' },
  { code: 'LGW', name: 'London Gatwick Airport', postcode: 'RH6 0NP', address: 'Horley, West Sussex' },
  { code: 'LBA', name: 'Leeds Bradford Airport', postcode: 'LS19 7TU', address: 'Whitehouse Lane, Yeadon, Leeds' },
  { code: 'LPL', name: 'Liverpool John Lennon Airport', postcode: 'L24 1YD', address: 'Liverpool, Speke Hall Avenue' },
  { code: 'BHX', name: 'Birmingham Airport', postcode: 'B26 3QJ', address: 'Birmingham, Solihull' },
  { code: 'EDI', name: 'Edinburgh Airport', postcode: 'EH12 9DN', address: 'Edinburgh, Scotland' },
  { code: 'GLA', name: 'Glasgow Airport', postcode: 'PA3 2SW', address: 'Paisley, Renfrewshire, Scotland' },
  { code: 'STN', name: 'London Stansted Airport', postcode: 'CM24 1QW', address: 'Bassingbourn Road, Stansted' },
  { code: 'LTN', name: 'London Luton Airport', postcode: 'LU2 9LY', address: 'Luton, Bedfordshire' }
];

const airportStmt = db.prepare(`
  INSERT OR REPLACE INTO cms_airports (code, name, postcode, address, meet_greet_fee, parking_fee, is_active)
  VALUES (?, ?, ?, ?, 15.00, 0.00, 1)
`);

airports.forEach(airport => {
  airportStmt.run(airport.code, airport.name, airport.postcode, airport.address);
});
airportStmt.finalize();
console.log(`✓ Seeded ${airports.length} airports`);

// 2. Seed Vehicles
const vehicles = [
  {
    name: 'Mercedes E-Class',
    description: 'Executive Sedan - Perfect for business and airport transfers',
    max_passengers: 3,
    max_luggage: 3,
    vehicle_type: 'executive',
    base_price: 45.00,
    price_per_mile: 1.50,
    image_url: '/mercedes-e-class.jpg',
    features: JSON.stringify(['Leather Seats', 'Climate Control', 'Wi-Fi', 'Phone Chargers'])
  },
  {
    name: 'Mercedes S-Class',
    description: 'Luxury Sedan - Ultimate comfort and style',
    max_passengers: 3,
    max_luggage: 3,
    vehicle_type: 'luxury',
    base_price: 65.00,
    price_per_mile: 2.00,
    image_url: '/mercedes-s-class.jpg',
    features: JSON.stringify(['Premium Leather', 'Massage Seats', 'Wi-Fi', 'Champagne Bar'])
  },
  {
    name: 'Mercedes V-Class',
    description: 'Premium MPV - Ideal for groups and families',
    max_passengers: 6,
    max_luggage: 6,
    vehicle_type: 'mpv',
    base_price: 55.00,
    price_per_mile: 1.80,
    image_url: '/mercedes-v-class.jpg',
    features: JSON.stringify(['Spacious Interior', 'Climate Control', 'Wi-Fi', 'Entertainment System'])
  },
  {
    name: 'Mercedes Sprinter',
    description: 'Executive Minibus - Perfect for large groups',
    max_passengers: 16,
    max_luggage: 16,
    vehicle_type: 'minibus',
    base_price: 85.00,
    price_per_mile: 2.50,
    image_url: '/mercedes-sprinter.jpg',
    features: JSON.stringify(['Large Capacity', 'Air Conditioning', 'USB Charging', 'Luggage Space'])
  },
  {
    name: 'Standard Saloon',
    description: 'Economy Option - Reliable and affordable',
    max_passengers: 4,
    max_luggage: 2,
    vehicle_type: 'standard',
    base_price: 35.00,
    price_per_mile: 1.20,
    image_url: '/standard-saloon.jpg',
    features: JSON.stringify(['Air Conditioning', 'GPS Navigation', 'Phone Chargers'])
  },
  {
    name: 'Estate Car',
    description: 'Extra Luggage Space - Great for airport runs',
    max_passengers: 4,
    max_luggage: 4,
    vehicle_type: 'estate',
    base_price: 40.00,
    price_per_mile: 1.35,
    image_url: '/estate-car.jpg',
    features: JSON.stringify(['Extra Luggage Space', 'Climate Control', 'GPS'])
  }
];

const vehicleStmt = db.prepare(`
  INSERT OR REPLACE INTO cms_vehicles 
  (name, description, max_passengers, max_luggage, vehicle_type, base_price, price_per_mile, image_url, features, is_active, display_order)
  VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 1, 0)
`);

let displayOrder = 1;
vehicles.forEach(v => {
  vehicleStmt.run(v.name, v.description, v.max_passengers, v.max_luggage, v.vehicle_type, v.base_price, v.price_per_mile, v.image_url, v.features);
  displayOrder++;
});
vehicleStmt.finalize();
console.log(`✓ Seeded ${vehicles.length} vehicles`);

// 3. Seed Pricing Rules
const pricingRules = [
  {
    rule_name: 'Return Journey Discount',
    rule_type: 'discount',
    value: 10,
    value_type: 'percentage',
    applicable_to: 'return_journey',
    description: '10% discount on return journeys'
  },
  {
    rule_name: 'Early Morning Surcharge',
    rule_type: 'surcharge',
    value: 5,
    value_type: 'percentage',
    applicable_to: 'time_based',
    description: '5% surcharge for pickups between midnight and 6 AM'
  },
  {
    rule_name: 'Late Night Surcharge',
    rule_type: 'surcharge',
    value: 5,
    value_type: 'percentage',
    applicable_to: 'time_based',
    description: '5% surcharge for pickups after 11 PM'
  },
  {
    rule_name: 'Weekend Surcharge',
    rule_type: 'surcharge',
    value: 10,
    value_type: 'percentage',
    applicable_to: 'weekend',
    description: '10% surcharge for weekend bookings'
  }
];

const pricingStmt = db.prepare(`
  INSERT OR REPLACE INTO cms_pricing
  (rule_name, rule_type, value, value_type, applicable_to, description, is_active)
  VALUES (?, ?, ?, ?, ?, ?, 1)
`);

pricingRules.forEach(rule => {
  pricingStmt.run(rule.rule_name, rule.rule_type, rule.value, rule.value_type, rule.applicable_to, rule.description);
});
pricingStmt.finalize();
console.log(`✓ Seeded ${pricingRules.length} pricing rules`);

// 4. Seed Website Settings
const settings = [
  { key: 'site_name', value: 'Ash Transfer', category: 'general', type: 'text' },
  { key: 'site_tagline', value: 'Mercedes E Class 2025 Transfers', category: 'general', type: 'text' },
  { key: 'contact_phone', value: '+44 7860 261578', category: 'contact', type: 'text' },
  { key: 'contact_email', value: 'info@ashtransfer.com', category: 'contact', type: 'text' },
  { key: 'whatsapp_number', value: '447860261578', category: 'contact', type: 'text' },
  { key: 'business_hours', value: '24/7/365', category: 'general', type: 'text' },
  { key: 'service_areas', value: 'Greater Manchester, Chester, Liverpool & Surrounding Areas', category: 'general', type: 'text' },
  { key: 'base_distance_rate', value: '1.50', category: 'pricing', type: 'number' },
  { key: 'base_time_rate', value: '0.50', category: 'pricing', type: 'number' },
  { key: 'minimum_fare', value: '25.00', category: 'pricing', type: 'number' },
  { key: 'currency', value: 'GBP', category: 'pricing', type: 'text' },
  { key: 'currency_symbol', value: '£', category: 'pricing', type: 'text' }
];

const settingsStmt = db.prepare(`
  INSERT OR REPLACE INTO cms_settings
  (setting_key, setting_value, setting_type, category, description)
  VALUES (?, ?, ?, ?, ?)
`);

settings.forEach(s => {
  settingsStmt.run(s.key, s.value, s.type, s.category, s.key.replace(/_/g, ' '));
});
settingsStmt.finalize();
console.log(`✓ Seeded ${settings.length} website settings`);

// 5. Seed Content Pages
const content = [
  {
    section: 'homepage_hero',
    title: 'Premium Mercedes Airport Transfers',
    subtitle: 'Professional, Reliable, Luxury Transport Service',
    content: '',
    button_text: 'Get A Quote',
    button_link: '/quotation.html',
    image_url: '',
    display_order: 1
  },
  {
    section: 'about_us',
    title: 'About Ash Transfer',
    subtitle: '',
    content: 'Ash Transfer provides premium Mercedes airport transfers across Greater Manchester and surrounding areas. Our fleet of executive vehicles ensures comfort, style, and reliability for all your transportation needs.',
    button_text: 'Learn More',
    button_link: '/fleet.html',
    image_url: '',
    display_order: 1
  },
  {
    section: 'services',
    title: 'Our Services',
    subtitle: 'Professional Transport Solutions',
    content: 'From airport transfers to corporate travel, we offer a complete range of premium transport services.',
    button_text: 'View Services',
    button_link: '/fleet.html',
    image_url: '',
    display_order: 1
  }
];

const contentStmt = db.prepare(`
  INSERT OR REPLACE INTO cms_content
  (section, title, subtitle, content, button_text, button_link, image_url, is_active, display_order)
  VALUES (?, ?, ?, ?, ?, ?, ?, 1, ?)
`);

content.forEach(c => {
  contentStmt.run(c.section, c.title, c.subtitle, c.content, c.button_text, c.button_link, c.image_url, c.display_order);
});
contentStmt.finalize();
console.log(`✓ Seeded ${content.length} content entries`);

db.close(() => {
  console.log('\n🎉 CMS data seeding complete!');
  console.log('\n📊 Summary:');
  console.log(`   - ${airports.length} airports`);
  console.log(`   - ${vehicles.length} vehicles`);
  console.log(`   - ${pricingRules.length} pricing rules`);
  console.log(`   - ${settings.length} website settings`);
  console.log(`   - ${content.length} content entries`);
});
