-- Employee field visits & prospective dealers
-- Run once against your TrackPro MySQL database

CREATE TABLE IF NOT EXISTS prospective_dealers (
  id INT AUTO_INCREMENT PRIMARY KEY,
  employee_user_id INT NOT NULL,
  shop_name VARCHAR(200) NOT NULL,
  contact_person VARCHAR(150),
  mobile VARCHAR(20),
  email VARCHAR(150),
  business_address TEXT,
  city VARCHAR(100),
  state VARCHAR(100),
  pin_code VARCHAR(10),
  remarks TEXT,
  business_photo_urls JSON,
  lead_status ENUM(
    'new',
    'contacted',
    'interested',
    'not_interested',
    'application_submitted',
    'converted',
    'lost'
  ) DEFAULT 'new',
  dealer_id INT DEFAULT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  FOREIGN KEY (employee_user_id) REFERENCES users(id) ON DELETE CASCADE,
  FOREIGN KEY (dealer_id) REFERENCES dealers(id) ON DELETE SET NULL,
  INDEX idx_prospect_employee (employee_user_id),
  INDEX idx_prospect_status (lead_status)
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS employee_visits (
  id INT AUTO_INCREMENT PRIMARY KEY,
  employee_user_id INT NOT NULL,
  prospective_dealer_id INT DEFAULT NULL,
  visit_role ENUM('dealer','distributor') NOT NULL DEFAULT 'dealer',
  prospective_distributor_id INT DEFAULT NULL,
  visit_date DATE NOT NULL,
  visit_time TIME DEFAULT NULL,
  shop_name VARCHAR(200),
  contact_person VARCHAR(150),
  mobile VARCHAR(20),
  meeting_notes TEXT,
  location_address TEXT,
  city VARCHAR(100),
  photo_urls JSON,
  lead_status VARCHAR(50) DEFAULT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  FOREIGN KEY (employee_user_id) REFERENCES users(id) ON DELETE CASCADE,
  FOREIGN KEY (prospective_dealer_id) REFERENCES prospective_dealers(id) ON DELETE SET NULL,
  FOREIGN KEY (prospective_distributor_id) REFERENCES prospective_distributors(id) ON DELETE SET NULL,
  INDEX idx_visits_employee (employee_user_id),
  INDEX idx_visits_date (visit_date)
) ENGINE=InnoDB;
