const { Pool } = require('pg');
require('dotenv').config();

const pool = new Pool({
    user: process.env.DB_USER,
    host: process.env.DB_HOST,
    database: process.env.DB_NAME,
    password: process.env.DB_PASSWORD,
    port: process.env.DB_PORT,
});

async function seed() {
    const adminId = 1;
    const carIds = [1, 2, 3, 4, 8, 9];

    const bookings = [
        {
            car_id: 1, // Tesla Model S
            customer: 'James Bond',
            email: 'james@bond.com',
            phone: '+44 7700 900007',
            address: 'Skyfall Lodge, Scotland',
            license: 'UK-007-SPECTRE',
            cc_holder: 'JAMES BOND',
            cc_last4: '0007',
            cc_expiry: '12/28',
            cc_type: 'Amex',
            discount_code: 'MI6-STAFF',
            discount_percent: 20,
            total_price: 450.00,
            status: 'confirmed',
            notes: 'Needs car delivered at night.'
        },
        {
            car_id: 2, // BMW M4
            customer: 'Dominic Toretto',
            email: 'dom@fast.family',
            phone: '+1 323 555 1010',
            address: '722 E Kensington Rd, LA',
            license: 'USA-CA-998822',
            cc_holder: 'DOMINIC TORETTO',
            cc_last4: '5566',
            cc_expiry: '05/27',
            cc_type: 'Visa',
            discount_code: 'FAMILY15',
            discount_percent: 15,
            total_price: 360.00,
            status: 'pending',
            notes: 'No nitro allowed in rental.'
        },
        {
            car_id: 3, // Audi A6
            customer: 'Frank Martin',
            email: 'the.transporter@gmail.com',
            phone: '+33 4 93 00 00 00',
            address: 'Nice, France',
            license: 'FR-TRANSP-01',
            cc_holder: 'FRANK MARTIN',
            cc_last4: '1020',
            cc_expiry: '01/26',
            cc_type: 'Mastercard',
            discount_code: 'ONTIME',
            discount_percent: 5,
            total_price: 285.00,
            status: 'confirmed',
            notes: 'Rules are rules.'
        },
        {
            car_id: 4, // Mercedes C-Class
            customer: 'Natasha Romanoff',
            email: 'black.widow@avengers.com',
            phone: '+1 917 555 4433',
            address: 'Stark Tower, NYC',
            license: 'USA-NY-RD-889',
            cc_holder: 'NATASHA ROMANOFF',
            cc_last4: '9902',
            cc_expiry: '09/25',
            cc_type: 'Visa',
            total_price: 190.00,
            status: 'cancelled',
            notes: 'Booking cancelled due to mission.'
        },
        {
            car_id: 8, // Tesla Model 1
            customer: 'Tony Stark',
            email: 'tony@starkindustries.com',
            phone: '+1 212 555 0199',
            address: 'Malibu, California',
            license: 'USA-CA-IRON-01',
            cc_holder: 'ANTHONY STARK',
            cc_last4: '1111',
            cc_expiry: '11/30',
            cc_type: 'Amex',
            discount_code: 'I_AM_IRONMAN',
            discount_percent: 50,
            total_price: 75.00,
            status: 'confirmed',
            notes: 'Autonomous driving must be active.'
        },
        {
            car_id: 9, // maruthi model 1
            customer: 'Raj Kumar',
            email: 'raj.kumar@email.in',
            phone: '+91 98765 43210',
            address: 'New Delhi, India',
            license: 'IND-DL-10992',
            cc_holder: 'RAJ KUMAR',
            cc_last4: '8877',
            cc_expiry: '02/26',
            cc_type: 'Mastercard',
            total_price: 120.00,
            status: 'pending',
            notes: 'Needs child seat.'
        }
    ];

    try {
        console.log('Inserting sample bookings...');
        for (const b of bookings) {
            const discountAmt = (b.total_price * (b.discount_percent || 0) / 100).toFixed(2);
            await pool.query(`
        INSERT INTO bookings (
          user_id, car_id, start_date, end_date, total_price, status,
          customer_name, customer_email, customer_phone, customer_address, customer_license,
          cc_holder, cc_last4, cc_expiry, cc_type,
          discount_code, discount_percent, discount_amount, base_price, notes
        ) VALUES (
          $1, $2, CURRENT_DATE, CURRENT_DATE + INTERVAL '3 days', $3, $4,
          $5, $6, $7, $8, $9,
          $10, $11, $12, $13,
          $14, $15, $16, $17, $18
        )
      `, [
                adminId, b.car_id, b.total_price, b.status,
                b.customer, b.email, b.phone, b.address, b.license,
                b.cc_holder, b.cc_last4, b.cc_expiry, b.cc_type,
                b.discount_code || null, b.discount_percent || 0, discountAmt, b.total_price, b.notes || null
            ]);
        }
        console.log('Sample bookings inserted successfully!');
    } catch (err) {
        console.error('Error seeding bookings:', err);
    } finally {
        await pool.end();
    }
}

seed();
