-- Adapted from user requirements for MySQL/MariaDB

CREATE TABLE IF NOT EXISTS `customers` (
  `id` INT AUTO_INCREMENT PRIMARY KEY,
  `first_name` VARCHAR(100),
  `last_name` VARCHAR(100),
  `email` VARCHAR(255) UNIQUE,
  `phone` VARCHAR(20),
  `phone_normalized` VARCHAR(15),
  `service_address` TEXT,
  `city` VARCHAR(100),
  `state` VARCHAR(50),
  `zip_code` VARCHAR(20),
  `lifetime_value` DECIMAL(10, 2) DEFAULT 0,
  `total_bookings` INT DEFAULT 0,
  `total_quotes` INT DEFAULT 0,
  `acquisition_source` ENUM('google_ads', 'google_lsa', 'organic', 'referral', 'facebook', 'yelp', 'direct', 'other'),
  `acquisition_campaign` VARCHAR(255),
  `first_contact_date` DATETIME DEFAULT CURRENT_TIMESTAMP,
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `bookings` (
  `id` INT AUTO_INCREMENT PRIMARY KEY,
  `record_type` ENUM('appointment', 'quote_request') NOT NULL,
  `customer_name` VARCHAR(255),
  `customer_phone` VARCHAR(20),
  `customer_email` VARCHAR(255),
  `service_address` TEXT,
  `service_category` ENUM('repair', 'maintenance', 'installation', 'inspection', 'emergency'),
  `service_type` VARCHAR(100),
  `system_type` ENUM('central_air', 'heat_pump', 'furnace', 'mini_split', 'boiler', 'other'),
  `urgency_level` ENUM('routine', 'urgent', 'emergency'),
  `issue_description` TEXT,
  `appointment_date` DATE,
  `appointment_time` VARCHAR(20),
  `status` ENUM('new', 'confirmed', 'dispatched', 'in_progress', 'completed', 'cancelled', 'no_show') NOT NULL DEFAULT 'new',
  `estimated_revenue` DECIMAL(10, 2),
  `actual_revenue` DECIMAL(10, 2),
  `booked_by` ENUM('ai_agent', 'human_agent', 'online', 'walk_in'),
  `customer_id` INT,
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  FOREIGN KEY (`customer_id`) REFERENCES `customers`(`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
