package stats

const DailyMileageTableSQL = `CREATE TABLE IF NOT EXISTS vehicle_daily_mileage (
  vin VARCHAR(32) NOT NULL,
  stat_date DATE NOT NULL,
  protocol VARCHAR(32) NOT NULL,
  source_id BIGINT NULL,
  daily_mileage_km DECIMAL(18,3) NOT NULL DEFAULT 0,
  latest_total_mileage_km DECIMAL(18,3) NULL,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (vin, stat_date, protocol),
  KEY idx_stat_date (stat_date),
  KEY idx_protocol_date (protocol, stat_date),
  KEY idx_source_id (source_id)
)`

var DailyMileageAlterSQL = []string{
	"ALTER TABLE vehicle_data_source ADD COLUMN source_code VARCHAR(64) NULL AFTER platform_name",
	"ALTER TABLE vehicle_data_source ADD COLUMN source_kind VARCHAR(32) NOT NULL DEFAULT 'UNKNOWN' AFTER source_code",
	"ALTER TABLE vehicle_data_source ADD KEY idx_protocol_source_code (protocol, source_code)",
	"ALTER TABLE vehicle_data_source ADD KEY idx_protocol_source_kind_seen (protocol, source_kind, latest_seen_at)",
	"ALTER TABLE vehicle_daily_mileage ADD COLUMN source_id BIGINT NULL AFTER protocol",
	"ALTER TABLE vehicle_daily_mileage ADD KEY idx_source_id (source_id)",
	"ALTER TABLE vehicle_daily_mileage DROP COLUMN first_total_mileage_km",
	"ALTER TABLE vehicle_daily_mileage DROP COLUMN trusted_source_key",
	"ALTER TABLE vehicle_daily_mileage DROP COLUMN trusted_phone",
	"ALTER TABLE vehicle_daily_mileage DROP COLUMN trusted_source_endpoint",
	"ALTER TABLE vehicle_daily_mileage DROP COLUMN sample_count",
	"ALTER TABLE vehicle_daily_mileage_source ADD KEY idx_protocol_date_selected (protocol, stat_date, is_selected, vin)",
	"ALTER TABLE vehicle_daily_mileage_source ADD KEY idx_protocol_date_quality (protocol, stat_date, quality_status, vin)",
	"ALTER TABLE vehicle_daily_mileage_source ADD KEY idx_protocol_date_quality_reason (protocol, stat_date, quality_status, quality_reason, vin)",
	"ALTER TABLE vehicle_daily_mileage_source ADD KEY idx_source_ip_date (protocol, source_ip, stat_date)",
}

const DataSourceTableSQL = `CREATE TABLE IF NOT EXISTS vehicle_data_source (
  id BIGINT NOT NULL AUTO_INCREMENT,
  protocol VARCHAR(32) NOT NULL,
  source_ip VARCHAR(64) NOT NULL,
  latest_source_endpoint VARCHAR(128) NULL,
  platform_name VARCHAR(128) NULL,
  source_code VARCHAR(64) NULL,
  source_kind VARCHAR(32) NOT NULL DEFAULT 'UNKNOWN',
  trust_priority INT NOT NULL DEFAULT 100,
  enabled TINYINT(1) NOT NULL DEFAULT 1,
  first_seen_at DATETIME NULL,
  latest_seen_at DATETIME NULL,
  remark VARCHAR(512) NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_protocol_source_ip (protocol, source_ip),
  KEY idx_protocol_source_code (protocol, source_code),
  KEY idx_protocol_source_kind_seen (protocol, source_kind, latest_seen_at),
  KEY idx_protocol_enabled_priority (protocol, enabled, trust_priority)
)`

const DailyMileageSourceTableSQL = `CREATE TABLE IF NOT EXISTS vehicle_daily_mileage_source (
  vin VARCHAR(32) NOT NULL,
  stat_date DATE NOT NULL,
  protocol VARCHAR(32) NOT NULL,
  source_key VARCHAR(256) NOT NULL,
  source_ip VARCHAR(64) NOT NULL,
  source_endpoint VARCHAR(128) NULL,
  phone VARCHAR(32) NULL,
  device_id VARCHAR(64) NULL,
  platform_name VARCHAR(128) NULL,
  first_total_mileage_km DECIMAL(18,3) NULL,
  latest_total_mileage_km DECIMAL(18,3) NULL,
  daily_mileage_km DECIMAL(18,3) NOT NULL DEFAULT 0,
  sample_count BIGINT NOT NULL DEFAULT 0,
  first_event_time DATETIME NULL,
  latest_event_time DATETIME NULL,
  quality_status VARCHAR(32) NOT NULL DEFAULT 'OK',
  quality_reason VARCHAR(255) NULL,
  is_selected TINYINT(1) NOT NULL DEFAULT 0,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (vin, stat_date, protocol, source_key),
  KEY idx_protocol_date (protocol, stat_date),
  KEY idx_source_ip (protocol, source_ip),
  KEY idx_selected (stat_date, protocol, is_selected),
  KEY idx_protocol_date_selected (protocol, stat_date, is_selected, vin),
  KEY idx_protocol_date_quality (protocol, stat_date, quality_status, vin),
  KEY idx_protocol_date_quality_reason (protocol, stat_date, quality_status, quality_reason, vin),
  KEY idx_source_ip_date (protocol, source_ip, stat_date)
)`
