-- ============================================================
-- DEALERS CSV IMPORT - WORKING SOLUTION
-- ============================================================
-- This script provides a working solution for importing dealers
-- from CSV with auto-generated columns (dealer_id, created_at, updated_at, is_active)
-- ============================================================

-- METHOD 1: If your CSV does NOT have dealer_id, created_at, updated_at, is_active
-- ============================================================
-- Use this COPY command if your CSV has exactly 44 columns (without the auto-generated ones)

\copy public.dealers (sales_code,service_code,gear_code,gma_code,region,dealership,state,district,city,location,city_category_pst,layout_format,tier_city_category,on_boarding_charges,"date",single_format_month_year,domain_id,replacement,termination_resignation_status,date_of_termination_resignation,last_date_of_operations,old_codes,branch_details,dealer_principal_name,dealer_principal_email_id,dp_contact_number,dp_contacts,showroom_address,showroom_pincode,workshop_address,workshop_pincode,location_district,state_workshop,no_of_studios,website_update,gst,pan,firm_type,prop_managing_partners_directors,total_prop_partners_directors,docs_folder_link,workshop_gma_codes,existing_new,dlrcode) FROM 'C:/Users/BACKPACKERS/Downloads/Dealer_Master.csv' CSV HEADER ENCODING 'WIN1252';


-- ============================================================
-- METHOD 2: If your CSV HAS dealer_id, created_at, updated_at, is_active columns
-- ============================================================
-- Use this approach if your CSV has 48 columns (including the auto-generated ones)
-- This creates a temporary table, imports, then inserts with defaults

-- Step 1: Create temporary table matching your CSV structure
-- This accepts ALL columns from CSV (whether 44 or 48 columns)
CREATE TEMP TABLE dealers_temp (
 dealer_id TEXT,
 sales_code TEXT,
 service_code TEXT,
 gear_code TEXT,
 gma_code TEXT,
 region TEXT,
 dealership TEXT,
 state TEXT,
 district TEXT,
 city TEXT,
 location TEXT,
 city_category_pst TEXT,
 layout_format TEXT,
 tier_city_category TEXT,
 on_boarding_charges TEXT,
 date TEXT,
 single_format_month_year TEXT,
 domain_id TEXT,
 replacement TEXT,
 termination_resignation_status TEXT,
 date_of_termination_resignation TEXT,
 last_date_of_operations TEXT,
 old_codes TEXT,
 branch_details TEXT,
 dealer_principal_name TEXT,
 dealer_principal_email_id TEXT,
 dp_contact_number TEXT,
 dp_contacts TEXT,
 showroom_address TEXT,
 showroom_pincode TEXT,
 workshop_address TEXT,
 workshop_pincode TEXT,
 location_district TEXT,
 state_workshop TEXT,
 no_of_studios TEXT,
 website_update TEXT,
 gst TEXT,
 pan TEXT,
 firm_type TEXT,
 prop_managing_partners_directors TEXT,
 total_prop_partners_directors TEXT,
 docs_folder_link TEXT,
 workshop_gma_codes TEXT,
 existing_new TEXT,
 dlrcode TEXT,
 created_at TEXT,
 updated_at TEXT,
 is_active TEXT
);

-- Step 2: Import CSV into temporary table
-- This will work whether your CSV has 44 or 48 columns
\copy dealers_temp FROM 'C:/Users/COMP/Downloads/DEALERS_CLEAN.csv' WITH (FORMAT csv, HEADER true, ENCODING 'UTF8');

-- Optional: Check what was imported
-- SELECT COUNT(*) FROM dealers_temp;

-- Step 3: Insert into actual dealers table
-- IMPORTANT: We IGNORE dealer_id, created_at, updated_at, is_active from CSV
-- These will use database DEFAULT values (auto-generated UUID, current timestamp, true)
INSERT INTO public.dealers (
 sales_code,
 service_code,
 gear_code,
 gma_code,
 region,
 dealership,
 state,
 district,
 city,
 location,
 city_category_pst,
 layout_format,
 tier_city_category,
 on_boarding_charges,
 date,
 single_format_month_year,
 domain_id,
 replacement,
 termination_resignation_status,
 date_of_termination_resignation,
 last_date_of_operations,
 old_codes,
 branch_details,
 dealer_principal_name,
 dealer_principal_email_id,
 dp_contact_number,
 dp_contacts,
 showroom_address,
 showroom_pincode,
 workshop_address,
 workshop_pincode,
 location_district,
 state_workshop,
 no_of_studios,
 website_update,
 gst,
 pan,
 firm_type,
 prop_managing_partners_directors,
 total_prop_partners_directors,
 docs_folder_link,
 workshop_gma_codes,
 existing_new,
 dlrcode
)
SELECT 
 NULLIF(sales_code, ''),
 NULLIF(service_code, ''),
 NULLIF(gear_code, ''),
 NULLIF(gma_code, ''),
 NULLIF(region, ''),
 NULLIF(dealership, ''),
 NULLIF(state, ''),
 NULLIF(district, ''),
 NULLIF(city, ''),
 NULLIF(location, ''),
 NULLIF(city_category_pst, ''),
 NULLIF(layout_format, ''),
 NULLIF(tier_city_category, ''),
 NULLIF(on_boarding_charges, ''),
 NULLIF(date, ''),
 NULLIF(single_format_month_year, ''),
 NULLIF(domain_id, ''),
 NULLIF(replacement, ''),
 NULLIF(termination_resignation_status, ''),
 NULLIF(date_of_termination_resignation, ''),
 NULLIF(last_date_of_operations, ''),
 NULLIF(old_codes, ''),
 NULLIF(branch_details, ''),
 NULLIF(dealer_principal_name, ''),
 NULLIF(dealer_principal_email_id, ''),
 NULLIF(dp_contact_number, ''),
 NULLIF(dp_contacts, ''),
 NULLIF(showroom_address, ''),
 NULLIF(showroom_pincode, ''),
 NULLIF(workshop_address, ''),
 NULLIF(workshop_pincode, ''),
 NULLIF(location_district, ''),
 NULLIF(state_workshop, ''),
 CASE WHEN no_of_studios = '' THEN 0 ELSE no_of_studios::INTEGER END,
 NULLIF(website_update, ''),
 NULLIF(gst, ''),
 NULLIF(pan, ''),
 NULLIF(firm_type, ''),
 NULLIF(prop_managing_partners_directors, ''),
 NULLIF(total_prop_partners_directors, ''),
 NULLIF(docs_folder_link, ''),
 NULLIF(workshop_gma_codes, ''),
 NULLIF(existing_new, ''),
 NULLIF(dlrcode, '')
FROM dealers_temp;

-- Step 4: Clean up temporary table
DROP TABLE dealers_temp;

-- ============================================================
-- METHOD 3: Using COPY with DEFAULT (PostgreSQL 12+)
-- ============================================================
-- Alternative approach using a function to set defaults

-- Create a function to handle the import with defaults
CREATE OR REPLACE FUNCTION import_dealers_from_csv()
RETURNS void AS $$
BEGIN
  -- This will be called from a COPY command that uses a function
  -- See METHOD 1 for the actual COPY command
END;
$$ LANGUAGE plpgsql;

-- ============================================================
-- VERIFICATION QUERIES
-- ============================================================

-- Check import results
SELECT 
  COUNT(*) as total_dealers,
  COUNT(dealer_id) as has_dealer_id,
  COUNT(created_at) as has_created_at,
  COUNT(updated_at) as has_updated_at,
  COUNT(*) FILTER (WHERE is_active = true) as active_count
FROM dealers;

-- View sample records with auto-generated values
SELECT 
  dealer_id,
  dlrcode,
  dealership,
  created_at,
  updated_at,
  is_active
FROM dealers
LIMIT 5;

-- Check for any issues
SELECT 
  COUNT(*) FILTER (WHERE dealer_id IS NULL) as missing_dealer_id,
  COUNT(*) FILTER (WHERE created_at IS NULL) as missing_created_at,
  COUNT(*) FILTER (WHERE updated_at IS NULL) as missing_updated_at
FROM dealers;

