const { Client } = require('pg');

const client = new Client({
  host: '66.116.207.225',
  port: 5433,
  database: 'nearledb',
  user: 'admin',
  password: 'Package@123#',
  ssl: false,
});

async function main() {
  try {
    await client.connect();
    console.log('Connected to PostgreSQL successfully!');

    // Let's execute a transaction test to insert the tenant and see the DB error
    await client.query('BEGIN');
    try {
      const query = `
        INSERT INTO tenants (
          tenantid, tenantname, configid, partnerid, moduleid, tenanttype, 
          registrationno, tenanttoken, companyname, devicetype, deviceid, 
          firstname, primaryemail, primarycontact, categoryid, subcategoryid, 
          address, suburb, city, state, postcode, latitude, longitude, 
          tenantimage, tenantinfo, paymode1, paymode2, promotion, minorder, 
          applocationid, approved, status, partneruserid
        ) VALUES (
          $1, $2, $3, $4, $5, $6, 
          $7, $8, $9, $10, $11, 
          $12, $13, $14, $15, $16, 
          $17, $18, $19, $20, $21, $22, $23, 
          $24, $25, $26, $27, $28, $29, 
          $30, $31, $32, $33
        )
      `;
      const values = [
        273, "Suriya Store", 1, 1, 2, "Retail",
        "REG-SURIYA-273", "fcm_token_suriya_273", "Suriya Enterprises", "Android", "device-uuid-9876543210",
        "Suriya", "suriya@example.com", "9876543212", 1, 2,
        "12, DB Road, RS Puram", "RS Puram", "Coimbatore", "Tamil Nadu", "641002", "11.0118", "76.9456",
        "https://example.com/suriya_store.jpg", "Groceries and essentials in RS Puram, Coimbatore", 1, 1, 0, 10,
        1, 0, "Active", 0
      ];

      await client.query(query, values);
      console.log('Tenant insert succeeded!');

      // Let's also try to insert the tenantlocation
      const locQuery = `
        INSERT INTO tenantlocations (
          tenantid, applocationid, moduleid, locationname, email, contactno,
          latitude, longitude, address, suburb, city, state, postcode,
          opentime, closetime, partnerid, deliveryradius, deliverymins, cancelsecs, status
        ) VALUES (
          $1, $2, $3, $4, $5, $6,
          $7, $8, $9, $10, $11, $12, $13,
          $14, $15, $16, $17, $18, $19, $20
        )
      `;
      const locValues = [
        273, 1, 2, "Suriya Store RS Puram", "rspuram@suriya.com", "9876543210",
        "11.0118", "76.9456", "12, DB Road, RS Puram", "RS Puram", "Coimbatore", "Tamil Nadu", "641002",
        "08:00", "22:00", 1, 5, 30, 120, "Active"
      ];
      await client.query(locQuery, locValues);
      console.log('Location insert succeeded!');

    } catch (e) {
      console.error('SQL Error during insert:', e.message, e.detail || '');
    } finally {
      await client.query('ROLLBACK');
    }

  } catch (err) {
    console.error('Error connecting:', err);
  } finally {
    await client.end();
  }
}

main();
