-- ============================================================
-- Doormile Seed Data — 4 Operating Zones
-- Idempotent: safe to re-run (uses WHERE NOT EXISTS guards)
-- ============================================================

-- ── STEP 0A: Fix sequences that drifted from explicit-ID inserts ──────────
SELECT setval('applocations_applocationid_seq',
    GREATEST((SELECT COALESCE(MAX(applocationid), 0) FROM applocations), 1), true);

SELECT setval('tenants_tenantid_seq',
    GREATEST((SELECT COALESCE(MAX(tenantid), 0) FROM tenants), 1), true);

SELECT setval('hubs_hubid_seq',
    GREATEST((SELECT COALESCE(MAX(hubid), 0) FROM hubs), 1), true);

SELECT setval('appusers_userid_seq',
    GREATEST((SELECT COALESCE(MAX(userid), 0) FROM appusers), 1), true);

SELECT setval('milerprofiles_milerprofileid_seq',
    GREATEST((SELECT COALESCE(MAX(milerprofileid), 0) FROM milerprofiles), 1), true);

SELECT setval('tenantlocations_tenantlocationid_seq',
    GREATEST((SELECT COALESCE(MAX(tenantlocationid), 0) FROM tenantlocations), 1), true);

-- ── STEP 0B: Add columns the Go backend expects but DB is missing ─────────
ALTER TABLE doormile_pricing
    ADD COLUMN IF NOT EXISTS provider_company  text    DEFAULT 'Doormile';

ALTER TABLE doormile_pricing
    ADD COLUMN IF NOT EXISTS reliability_score numeric DEFAULT 5.0;

ALTER TABLE milerprofiles
    ADD COLUMN IF NOT EXISTS device_token text;

ALTER TABLE appcustomers
    ADD COLUMN IF NOT EXISTS device_token text;

-- ══════════════════════════════════════════════════════════════
-- DATA INSERTS — all guarded with WHERE NOT EXISTS
-- ══════════════════════════════════════════════════════════════
BEGIN;

-- ── SECTION 1: APPLOCATION — Chennai ─────────────────────────────────────
INSERT INTO applocations (applocationname, status)
SELECT 'Chennai', 'Active'
WHERE NOT EXISTS (SELECT 1 FROM applocations WHERE applocationname = 'Chennai');

-- ── SECTION 2: TENANTS ───────────────────────────────────────────────────

-- Chennai operational tenant
INSERT INTO tenants (tenantname, primaryemail, primarycontact, status)
SELECT 'Doormile Chennai Logistics', 'chn-ops@doormile.com', '9876543215', 'Active'
WHERE NOT EXISTS (SELECT 1 FROM tenants WHERE tenantname = 'Doormile Chennai Logistics');

-- Provider/partner tenants (2 per zone, 8 total)
INSERT INTO tenants (tenantname, primaryemail, primarycontact, status)
SELECT 'KPM Travels', 'ops@kpmtravels.in', '9876500100', 'Active'
WHERE NOT EXISTS (SELECT 1 FROM tenants WHERE tenantname = 'KPM Travels');

INSERT INTO tenants (tenantname, primaryemail, primarycontact, status)
SELECT 'Sri Murugan Logistics', 'ops@srimuruganlogistics.in', '9876500101', 'Active'
WHERE NOT EXISTS (SELECT 1 FROM tenants WHERE tenantname = 'Sri Murugan Logistics');

INSERT INTO tenants (tenantname, primaryemail, primarycontact, status)
SELECT 'Namma Logistics', 'ops@nammalogistics.in', '9876500102', 'Active'
WHERE NOT EXISTS (SELECT 1 FROM tenants WHERE tenantname = 'Namma Logistics');

INSERT INTO tenants (tenantname, primaryemail, primarycontact, status)
SELECT 'Silicon Valley Couriers', 'ops@svccouriers.in', '9876500103', 'Active'
WHERE NOT EXISTS (SELECT 1 FROM tenants WHERE tenantname = 'Silicon Valley Couriers');

INSERT INTO tenants (tenantname, primaryemail, primarycontact, status)
SELECT 'Deccan Express', 'ops@deccanexpress.in', '9876500104', 'Active'
WHERE NOT EXISTS (SELECT 1 FROM tenants WHERE tenantname = 'Deccan Express');

INSERT INTO tenants (tenantname, primaryemail, primarycontact, status)
SELECT 'Charminar Logistics', 'ops@charminarlogs.in', '9876500105', 'Active'
WHERE NOT EXISTS (SELECT 1 FROM tenants WHERE tenantname = 'Charminar Logistics');

INSERT INTO tenants (tenantname, primaryemail, primarycontact, status)
SELECT 'Marina Logistics', 'ops@marinalogistics.in', '9876500106', 'Active'
WHERE NOT EXISTS (SELECT 1 FROM tenants WHERE tenantname = 'Marina Logistics');

INSERT INTO tenants (tenantname, primaryemail, primarycontact, status)
SELECT 'Ponni Couriers', 'ops@ponnicouriers.in', '9876500107', 'Active'
WHERE NOT EXISTS (SELECT 1 FROM tenants WHERE tenantname = 'Ponni Couriers');

-- ── SECTION 3: TENANT LOCATIONS ──────────────────────────────────────────
INSERT INTO tenantlocations (tenantid, address, city, state, pincode, latitude, longitude, isprimary, status)
SELECT t.tenantid, v.address, v.city, v.state, v.pincode, v.lat, v.lon, true, 'Active'
FROM tenants t
JOIN (VALUES
    ('KPM Travels',             'Gandhipuram, Coimbatore',  'Coimbatore', 'Tamil Nadu', '641012', 11.0168, 76.9558),
    ('Sri Murugan Logistics',   'Peelamedu, Coimbatore',    'Coimbatore', 'Tamil Nadu', '641004', 11.0301, 77.0034),
    ('Namma Logistics',         'Koramangala, Bengaluru',   'Bengaluru',  'Karnataka',  '560034', 12.9352, 77.6244),
    ('Silicon Valley Couriers', 'Whitefield, Bengaluru',    'Bengaluru',  'Karnataka',  '560066', 12.9698, 77.7499),
    ('Deccan Express',          'Gachibowli, Hyderabad',    'Hyderabad',  'Telangana',  '500032', 17.4483, 78.3741),
    ('Charminar Logistics',     'Banjara Hills, Hyderabad', 'Hyderabad',  'Telangana',  '500034', 17.4153, 78.4384),
    ('Marina Logistics',        'T Nagar, Chennai',         'Chennai',    'Tamil Nadu', '600017', 13.0418, 80.2341),
    ('Ponni Couriers',          'Velachery, Chennai',       'Chennai',    'Tamil Nadu', '600042', 12.9815, 80.2180)
) AS v(name, address, city, state, pincode, lat, lon)
    ON t.tenantname = v.name
WHERE NOT EXISTS (
    SELECT 1 FROM tenantlocations tl WHERE tl.tenantid = t.tenantid
);

-- ── SECTION 4: HUBS ──────────────────────────────────────────────────────
-- Coimbatore: RS Puram + Saravanampatti (existing: Gandhipuram sorting + Peelamedu delivery)
INSERT INTO hubs (hubname, hubtype, applocationid, address, latitude, longitude, pincode, status, contactno)
SELECT 'Coimbatore RS Puram Hub', 'delivery_hub', 1, 'RS Puram, Coimbatore', 11.0051, 76.9602, '641002', 'Active', '9876500200'
WHERE NOT EXISTS (SELECT 1 FROM hubs WHERE hubname = 'Coimbatore RS Puram Hub');

INSERT INTO hubs (hubname, hubtype, applocationid, address, latitude, longitude, pincode, status, contactno)
SELECT 'Coimbatore Saravanampatti Hub', 'delivery_hub', 1, 'Saravanampatti, Coimbatore', 11.0639, 77.0109, '641035', 'Active', '9876500201'
WHERE NOT EXISTS (SELECT 1 FROM hubs WHERE hubname = 'Coimbatore Saravanampatti Hub');

-- Hyderabad: Banjara Hills + Secunderabad (existing: Gachibowli sorting + Madhapur delivery)
INSERT INTO hubs (hubname, hubtype, applocationid, address, latitude, longitude, pincode, status, contactno)
SELECT 'Hyderabad Banjara Hills Hub', 'delivery_hub', 2, 'Banjara Hills, Hyderabad', 17.4153, 78.4384, '500034', 'Active', '9876500202'
WHERE NOT EXISTS (SELECT 1 FROM hubs WHERE hubname = 'Hyderabad Banjara Hills Hub');

INSERT INTO hubs (hubname, hubtype, applocationid, address, latitude, longitude, pincode, status, contactno)
SELECT 'Hyderabad Secunderabad Hub', 'delivery_hub', 2, 'Secunderabad, Hyderabad', 17.4399, 78.4983, '500003', 'Active', '9876500203'
WHERE NOT EXISTS (SELECT 1 FROM hubs WHERE hubname = 'Hyderabad Secunderabad Hub');

-- Bengaluru: Whitefield + Electronic City (existing: Koramangala sorting + Indiranagar delivery)
INSERT INTO hubs (hubname, hubtype, applocationid, address, latitude, longitude, pincode, status, contactno)
SELECT 'Bangalore Whitefield Hub', 'delivery_hub', 3, 'Whitefield, Bengaluru', 12.9698, 77.7499, '560066', 'Active', '9876500204'
WHERE NOT EXISTS (SELECT 1 FROM hubs WHERE hubname = 'Bangalore Whitefield Hub');

INSERT INTO hubs (hubname, hubtype, applocationid, address, latitude, longitude, pincode, status, contactno)
SELECT 'Bangalore Electronic City Hub', 'delivery_hub', 3, 'Electronic City, Bengaluru', 12.8399, 77.6770, '560100', 'Active', '9876500205'
WHERE NOT EXISTS (SELECT 1 FROM hubs WHERE hubname = 'Bangalore Electronic City Hub');

-- Chennai: 1 sorting_center + 3 delivery_hubs (all new)
INSERT INTO hubs (hubname, hubtype, applocationid, address, latitude, longitude, pincode, status, contactno)
SELECT 'Chennai T Nagar Hub', 'sorting_center',
    (SELECT applocationid FROM applocations WHERE applocationname = 'Chennai'),
    'T Nagar, Chennai', 13.0418, 80.2341, '600017', 'Active', '9876500206'
WHERE NOT EXISTS (SELECT 1 FROM hubs WHERE hubname = 'Chennai T Nagar Hub');

INSERT INTO hubs (hubname, hubtype, applocationid, address, latitude, longitude, pincode, status, contactno)
SELECT 'Chennai Anna Nagar Hub', 'delivery_hub',
    (SELECT applocationid FROM applocations WHERE applocationname = 'Chennai'),
    'Anna Nagar, Chennai', 13.0891, 80.2094, '600040', 'Active', '9876500207'
WHERE NOT EXISTS (SELECT 1 FROM hubs WHERE hubname = 'Chennai Anna Nagar Hub');

INSERT INTO hubs (hubname, hubtype, applocationid, address, latitude, longitude, pincode, status, contactno)
SELECT 'Chennai Velachery Hub', 'delivery_hub',
    (SELECT applocationid FROM applocations WHERE applocationname = 'Chennai'),
    'Velachery, Chennai', 12.9815, 80.2180, '600042', 'Active', '9876500208'
WHERE NOT EXISTS (SELECT 1 FROM hubs WHERE hubname = 'Chennai Velachery Hub');

INSERT INTO hubs (hubname, hubtype, applocationid, address, latitude, longitude, pincode, status, contactno)
SELECT 'Chennai Guindy Hub', 'delivery_hub',
    (SELECT applocationid FROM applocations WHERE applocationname = 'Chennai'),
    'Guindy, Chennai', 13.0067, 80.2206, '600032', 'Active', '9876500209'
WHERE NOT EXISTS (SELECT 1 FROM hubs WHERE hubname = 'Chennai Guindy Hub');

-- ── SECTION 5: MILERS ────────────────────────────────────────────────────
-- Password = [REDACTED]  (bcrypt hash matches existing milers in the DB)
-- Pattern: INSERT appusers WHERE phone not taken → RETURNING userid → INSERT milerprofiles

-- ── COIMBATORE — 3 new (existing: Ramesh 9876543255, Rajesh 9876543256, Karthi 9876543257) ──

WITH u AS (
    INSERT INTO appusers (authname, email, contactno, password, roleid, applocationid, tenantid, configid, status)
    SELECT 'Murugan Palani', 'murugan@doormile.com', '9876500001',
           '$2a$10$UUfjthIGXgA3y.1tFwpB..qx40ZmmUTHTQup5NQuJxHopVdNSgV4u', 5, 1, 1, 1001, 'Active'
    WHERE NOT EXISTS (SELECT 1 FROM appusers WHERE contactno = '9876500001')
    RETURNING userid
)
INSERT INTO milerprofiles (userid, displayname, phone, defaultvehicletype, currentlatitude, currentlongitude, currentpincode, availabilitystatus, rating, applocationid)
SELECT u.userid, 'Murugan CBE Rider', '9876500001', 'Bike', 11.0055, 76.9608, '641002', 'Available', 5.00, 1 FROM u
WHERE NOT EXISTS (SELECT 1 FROM milerprofiles WHERE phone = '9876500001');

WITH u AS (
    INSERT INTO appusers (authname, email, contactno, password, roleid, applocationid, tenantid, configid, status)
    SELECT 'Selvam Raja', 'selvam@doormile.com', '9876500002',
           '$2a$10$UUfjthIGXgA3y.1tFwpB..qx40ZmmUTHTQup5NQuJxHopVdNSgV4u', 5, 1, 1, 1001, 'Active'
    WHERE NOT EXISTS (SELECT 1 FROM appusers WHERE contactno = '9876500002')
    RETURNING userid
)
INSERT INTO milerprofiles (userid, displayname, phone, defaultvehicletype, currentlatitude, currentlongitude, currentpincode, availabilitystatus, rating, applocationid)
SELECT u.userid, 'Selvam CBE Rider', '9876500002', 'Bike', 11.0645, 77.0115, '641035', 'Available', 5.00, 1 FROM u
WHERE NOT EXISTS (SELECT 1 FROM milerprofiles WHERE phone = '9876500002');

WITH u AS (
    INSERT INTO appusers (authname, email, contactno, password, roleid, applocationid, tenantid, configid, status)
    SELECT 'Karthikeyan Vel', 'karthikvel@doormile.com', '9876500003',
           '$2a$10$UUfjthIGXgA3y.1tFwpB..qx40ZmmUTHTQup5NQuJxHopVdNSgV4u', 5, 1, 1, 1001, 'Active'
    WHERE NOT EXISTS (SELECT 1 FROM appusers WHERE contactno = '9876500003')
    RETURNING userid
)
INSERT INTO milerprofiles (userid, displayname, phone, defaultvehicletype, currentlatitude, currentlongitude, currentpincode, availabilitystatus, rating, applocationid)
SELECT u.userid, 'Karthikeyan CBE Rider', '9876500003', 'Bike', 11.0042, 76.9595, '641002', 'Available', 5.00, 1 FROM u
WHERE NOT EXISTS (SELECT 1 FROM milerprofiles WHERE phone = '9876500003');

-- ── HYDERABAD — 3 new (existing: Suresh 9876543261, Ali 9876543262, Venkatesh 9876543263) ──

WITH u AS (
    INSERT INTO appusers (authname, email, contactno, password, roleid, applocationid, tenantid, configid, status)
    SELECT 'Prasad Reddy', 'prasad@doormile.com', '9876500004',
           '$2a$10$UUfjthIGXgA3y.1tFwpB..qx40ZmmUTHTQup5NQuJxHopVdNSgV4u', 5, 2, 2, 1001, 'Active'
    WHERE NOT EXISTS (SELECT 1 FROM appusers WHERE contactno = '9876500004')
    RETURNING userid
)
INSERT INTO milerprofiles (userid, displayname, phone, defaultvehicletype, currentlatitude, currentlongitude, currentpincode, availabilitystatus, rating, applocationid)
SELECT u.userid, 'Prasad HYD Rider', '9876500004', 'Bike', 17.4158, 78.4390, '500034', 'Available', 5.00, 2 FROM u
WHERE NOT EXISTS (SELECT 1 FROM milerprofiles WHERE phone = '9876500004');

WITH u AS (
    INSERT INTO appusers (authname, email, contactno, password, roleid, applocationid, tenantid, configid, status)
    SELECT 'Naveen Kumar', 'naveen@doormile.com', '9876500005',
           '$2a$10$UUfjthIGXgA3y.1tFwpB..qx40ZmmUTHTQup5NQuJxHopVdNSgV4u', 5, 2, 2, 1001, 'Active'
    WHERE NOT EXISTS (SELECT 1 FROM appusers WHERE contactno = '9876500005')
    RETURNING userid
)
INSERT INTO milerprofiles (userid, displayname, phone, defaultvehicletype, currentlatitude, currentlongitude, currentpincode, availabilitystatus, rating, applocationid)
SELECT u.userid, 'Naveen HYD Rider', '9876500005', 'Bike', 17.4405, 78.4990, '500003', 'Available', 5.00, 2 FROM u
WHERE NOT EXISTS (SELECT 1 FROM milerprofiles WHERE phone = '9876500005');

WITH u AS (
    INSERT INTO appusers (authname, email, contactno, password, roleid, applocationid, tenantid, configid, status)
    SELECT 'Ravi Shankar', 'ravishankar@doormile.com', '9876500006',
           '$2a$10$UUfjthIGXgA3y.1tFwpB..qx40ZmmUTHTQup5NQuJxHopVdNSgV4u', 5, 2, 2, 1001, 'Active'
    WHERE NOT EXISTS (SELECT 1 FROM appusers WHERE contactno = '9876500006')
    RETURNING userid
)
INSERT INTO milerprofiles (userid, displayname, phone, defaultvehicletype, currentlatitude, currentlongitude, currentpincode, availabilitystatus, rating, applocationid)
SELECT u.userid, 'Ravi HYD Rider', '9876500006', 'Auto', 17.4162, 78.4378, '500034', 'Available', 5.00, 2 FROM u
WHERE NOT EXISTS (SELECT 1 FROM milerprofiles WHERE phone = '9876500006');

-- ── BENGALURU — 3 new (existing: Sanjay 9876543266, Anil 9876543271, Vijay 9876543272) ──

WITH u AS (
    INSERT INTO appusers (authname, email, contactno, password, roleid, applocationid, tenantid, configid, status)
    SELECT 'Ravi Gowda', 'ravigowda@doormile.com', '9876500007',
           '$2a$10$UUfjthIGXgA3y.1tFwpB..qx40ZmmUTHTQup5NQuJxHopVdNSgV4u', 5, 3, 3, 1001, 'Active'
    WHERE NOT EXISTS (SELECT 1 FROM appusers WHERE contactno = '9876500007')
    RETURNING userid
)
INSERT INTO milerprofiles (userid, displayname, phone, defaultvehicletype, currentlatitude, currentlongitude, currentpincode, availabilitystatus, rating, applocationid)
SELECT u.userid, 'Ravi BLR Rider', '9876500007', 'Bike', 12.9705, 77.7508, '560066', 'Available', 5.00, 3 FROM u
WHERE NOT EXISTS (SELECT 1 FROM milerprofiles WHERE phone = '9876500007');

WITH u AS (
    INSERT INTO appusers (authname, email, contactno, password, roleid, applocationid, tenantid, configid, status)
    SELECT 'Suresh Naik', 'sureshnaik@doormile.com', '9876500008',
           '$2a$10$UUfjthIGXgA3y.1tFwpB..qx40ZmmUTHTQup5NQuJxHopVdNSgV4u', 5, 3, 3, 1001, 'Active'
    WHERE NOT EXISTS (SELECT 1 FROM appusers WHERE contactno = '9876500008')
    RETURNING userid
)
INSERT INTO milerprofiles (userid, displayname, phone, defaultvehicletype, currentlatitude, currentlongitude, currentpincode, availabilitystatus, rating, applocationid)
SELECT u.userid, 'Suresh BLR Rider', '9876500008', 'Bike', 12.8405, 77.6778, '560100', 'Available', 5.00, 3 FROM u
WHERE NOT EXISTS (SELECT 1 FROM milerprofiles WHERE phone = '9876500008');

WITH u AS (
    INSERT INTO appusers (authname, email, contactno, password, roleid, applocationid, tenantid, configid, status)
    SELECT 'Mahesh Nair', 'maheshnair@doormile.com', '9876500009',
           '$2a$10$UUfjthIGXgA3y.1tFwpB..qx40ZmmUTHTQup5NQuJxHopVdNSgV4u', 5, 3, 3, 1001, 'Active'
    WHERE NOT EXISTS (SELECT 1 FROM appusers WHERE contactno = '9876500009')
    RETURNING userid
)
INSERT INTO milerprofiles (userid, displayname, phone, defaultvehicletype, currentlatitude, currentlongitude, currentpincode, availabilitystatus, rating, applocationid)
SELECT u.userid, 'Mahesh BLR Rider', '9876500009', 'Bike', 12.9712, 77.7495, '560066', 'Available', 5.00, 3 FROM u
WHERE NOT EXISTS (SELECT 1 FROM milerprofiles WHERE phone = '9876500009');

-- ── CHENNAI — 6 new milers (all new city) ────────────────────────────────

WITH u AS (
    INSERT INTO appusers (authname, email, contactno, password, roleid, applocationid, tenantid, configid, status)
    SELECT 'Prakash Kumar', 'prakash@doormile.com', '9876500010',
           '$2a$10$UUfjthIGXgA3y.1tFwpB..qx40ZmmUTHTQup5NQuJxHopVdNSgV4u', 5,
           (SELECT applocationid FROM applocations WHERE applocationname='Chennai'),
           (SELECT tenantid FROM tenants WHERE tenantname='Doormile Chennai Logistics'),
           1001, 'Active'
    WHERE NOT EXISTS (SELECT 1 FROM appusers WHERE contactno = '9876500010')
    RETURNING userid
)
INSERT INTO milerprofiles (userid, displayname, phone, defaultvehicletype, currentlatitude, currentlongitude, currentpincode, availabilitystatus, rating, applocationid)
SELECT u.userid, 'Prakash CHN Rider', '9876500010', 'Bike', 13.0422, 80.2348, '600017', 'Available', 5.00,
       (SELECT applocationid FROM applocations WHERE applocationname='Chennai')
FROM u
WHERE NOT EXISTS (SELECT 1 FROM milerprofiles WHERE phone = '9876500010');

WITH u AS (
    INSERT INTO appusers (authname, email, contactno, password, roleid, applocationid, tenantid, configid, status)
    SELECT 'Senthil Nathan', 'senthil@doormile.com', '9876500011',
           '$2a$10$UUfjthIGXgA3y.1tFwpB..qx40ZmmUTHTQup5NQuJxHopVdNSgV4u', 5,
           (SELECT applocationid FROM applocations WHERE applocationname='Chennai'),
           (SELECT tenantid FROM tenants WHERE tenantname='Doormile Chennai Logistics'),
           1001, 'Active'
    WHERE NOT EXISTS (SELECT 1 FROM appusers WHERE contactno = '9876500011')
    RETURNING userid
)
INSERT INTO milerprofiles (userid, displayname, phone, defaultvehicletype, currentlatitude, currentlongitude, currentpincode, availabilitystatus, rating, applocationid)
SELECT u.userid, 'Senthil CHN Rider', '9876500011', 'Bike', 13.0898, 80.2101, '600040', 'Available', 5.00,
       (SELECT applocationid FROM applocations WHERE applocationname='Chennai')
FROM u
WHERE NOT EXISTS (SELECT 1 FROM milerprofiles WHERE phone = '9876500011');

WITH u AS (
    INSERT INTO appusers (authname, email, contactno, password, roleid, applocationid, tenantid, configid, status)
    SELECT 'Mani Raja', 'maniraja@doormile.com', '9876500012',
           '$2a$10$UUfjthIGXgA3y.1tFwpB..qx40ZmmUTHTQup5NQuJxHopVdNSgV4u', 5,
           (SELECT applocationid FROM applocations WHERE applocationname='Chennai'),
           (SELECT tenantid FROM tenants WHERE tenantname='Doormile Chennai Logistics'),
           1001, 'Active'
    WHERE NOT EXISTS (SELECT 1 FROM appusers WHERE contactno = '9876500012')
    RETURNING userid
)
INSERT INTO milerprofiles (userid, displayname, phone, defaultvehicletype, currentlatitude, currentlongitude, currentpincode, availabilitystatus, rating, applocationid)
SELECT u.userid, 'Mani CHN Rider', '9876500012', 'Bike', 12.9820, 80.2185, '600042', 'Available', 5.00,
       (SELECT applocationid FROM applocations WHERE applocationname='Chennai')
FROM u
WHERE NOT EXISTS (SELECT 1 FROM milerprofiles WHERE phone = '9876500012');

WITH u AS (
    INSERT INTO appusers (authname, email, contactno, password, roleid, applocationid, tenantid, configid, status)
    SELECT 'Deepak Shankar', 'deepak@doormile.com', '9876500013',
           '$2a$10$UUfjthIGXgA3y.1tFwpB..qx40ZmmUTHTQup5NQuJxHopVdNSgV4u', 5,
           (SELECT applocationid FROM applocations WHERE applocationname='Chennai'),
           (SELECT tenantid FROM tenants WHERE tenantname='Doormile Chennai Logistics'),
           1001, 'Active'
    WHERE NOT EXISTS (SELECT 1 FROM appusers WHERE contactno = '9876500013')
    RETURNING userid
)
INSERT INTO milerprofiles (userid, displayname, phone, defaultvehicletype, currentlatitude, currentlongitude, currentpincode, availabilitystatus, rating, applocationid)
SELECT u.userid, 'Deepak CHN Rider', '9876500013', 'Auto', 13.0072, 80.2210, '600032', 'Available', 5.00,
       (SELECT applocationid FROM applocations WHERE applocationname='Chennai')
FROM u
WHERE NOT EXISTS (SELECT 1 FROM milerprofiles WHERE phone = '9876500013');

WITH u AS (
    INSERT INTO appusers (authname, email, contactno, password, roleid, applocationid, tenantid, configid, status)
    SELECT 'Kiran Vel', 'kiranvel@doormile.com', '9876500014',
           '$2a$10$UUfjthIGXgA3y.1tFwpB..qx40ZmmUTHTQup5NQuJxHopVdNSgV4u', 5,
           (SELECT applocationid FROM applocations WHERE applocationname='Chennai'),
           (SELECT tenantid FROM tenants WHERE tenantname='Doormile Chennai Logistics'),
           1001, 'Active'
    WHERE NOT EXISTS (SELECT 1 FROM appusers WHERE contactno = '9876500014')
    RETURNING userid
)
INSERT INTO milerprofiles (userid, displayname, phone, defaultvehicletype, currentlatitude, currentlongitude, currentpincode, availabilitystatus, rating, applocationid)
SELECT u.userid, 'Kiran CHN Rider', '9876500014', 'Bike', 13.0430, 80.2352, '600017', 'Available', 5.00,
       (SELECT applocationid FROM applocations WHERE applocationname='Chennai')
FROM u
WHERE NOT EXISTS (SELECT 1 FROM milerprofiles WHERE phone = '9876500014');

WITH u AS (
    INSERT INTO appusers (authname, email, contactno, password, roleid, applocationid, tenantid, configid, status)
    SELECT 'Arjun Raj', 'arjunraj@doormile.com', '9876500015',
           '$2a$10$UUfjthIGXgA3y.1tFwpB..qx40ZmmUTHTQup5NQuJxHopVdNSgV4u', 5,
           (SELECT applocationid FROM applocations WHERE applocationname='Chennai'),
           (SELECT tenantid FROM tenants WHERE tenantname='Doormile Chennai Logistics'),
           1001, 'Active'
    WHERE NOT EXISTS (SELECT 1 FROM appusers WHERE contactno = '9876500015')
    RETURNING userid
)
INSERT INTO milerprofiles (userid, displayname, phone, defaultvehicletype, currentlatitude, currentlongitude, currentpincode, availabilitystatus, rating, applocationid)
SELECT u.userid, 'Arjun CHN Rider', '9876500015', 'Bike', 13.0902, 80.2105, '600040', 'Available', 5.00,
       (SELECT applocationid FROM applocations WHERE applocationname='Chennai')
FROM u
WHERE NOT EXISTS (SELECT 1 FROM milerprofiles WHERE phone = '9876500015');

COMMIT;
