CREATE TABLE IF NOT EXISTS api_call_logs (
  id VARCHAR(36) NOT NULL,
  provider VARCHAR(64) NOT NULL DEFAULT 'biedawo',
  act VARCHAR(80) NOT NULL,
  endpoint VARCHAR(255) NOT NULL,
  method VARCHAR(12) NOT NULL,
  request_payload JSON NULL,
  response_payload JSON NULL,
  success TINYINT NOT NULL DEFAULT 0,
  status VARCHAR(16) NOT NULL DEFAULT 'failure',
  status_code INT NULL,
  duration_ms INT NULL,
  error_message TEXT NULL,
  created_at DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
  PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

SET @api_call_logs_provider_exists := (
  SELECT COUNT(*) FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'api_call_logs'
    AND column_name = 'provider'
);
SET @api_call_logs_provider_sql := IF(
  @api_call_logs_provider_exists = 0,
  'ALTER TABLE api_call_logs ADD COLUMN provider VARCHAR(64) NOT NULL DEFAULT ''biedawo'' AFTER id',
  'SELECT 1'
);
PREPARE api_call_logs_provider_stmt FROM @api_call_logs_provider_sql;
EXECUTE api_call_logs_provider_stmt;
DEALLOCATE PREPARE api_call_logs_provider_stmt;

SET @api_call_logs_status_exists := (
  SELECT COUNT(*) FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'api_call_logs'
    AND column_name = 'status'
);
SET @api_call_logs_status_sql := IF(
  @api_call_logs_status_exists = 0,
  'ALTER TABLE api_call_logs ADD COLUMN status VARCHAR(16) NOT NULL DEFAULT ''failure'' AFTER success',
  'SELECT 1'
);
PREPARE api_call_logs_status_stmt FROM @api_call_logs_status_sql;
EXECUTE api_call_logs_status_stmt;
DEALLOCATE PREPARE api_call_logs_status_stmt;

SET @api_call_logs_endpoint_exists := (
  SELECT COUNT(*) FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'api_call_logs'
    AND column_name = 'endpoint'
);
SET @api_call_logs_endpoint_sql := IF(
  @api_call_logs_endpoint_exists = 0,
  'ALTER TABLE api_call_logs ADD COLUMN endpoint VARCHAR(255) NOT NULL DEFAULT '''' AFTER act',
  'SELECT 1'
);
PREPARE api_call_logs_endpoint_stmt FROM @api_call_logs_endpoint_sql;
EXECUTE api_call_logs_endpoint_stmt;
DEALLOCATE PREPARE api_call_logs_endpoint_stmt;

SET @api_call_logs_success_exists := (
  SELECT COUNT(*) FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'api_call_logs'
    AND column_name = 'success'
);
SET @api_call_logs_success_sql := IF(
  @api_call_logs_success_exists = 0,
  'ALTER TABLE api_call_logs ADD COLUMN success TINYINT NOT NULL DEFAULT 0 AFTER response_payload',
  'SELECT 1'
);
PREPARE api_call_logs_success_stmt FROM @api_call_logs_success_sql;
EXECUTE api_call_logs_success_stmt;
DEALLOCATE PREPARE api_call_logs_success_stmt;

SET @api_call_logs_status_code_exists := (
  SELECT COUNT(*) FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'api_call_logs'
    AND column_name = 'status_code'
);
SET @api_call_logs_status_code_sql := IF(
  @api_call_logs_status_code_exists = 0,
  'ALTER TABLE api_call_logs ADD COLUMN status_code INT NULL AFTER status',
  'SELECT 1'
);
PREPARE api_call_logs_status_code_stmt FROM @api_call_logs_status_code_sql;
EXECUTE api_call_logs_status_code_stmt;
DEALLOCATE PREPARE api_call_logs_status_code_stmt;

SET @api_call_logs_created_at_index_exists := (
  SELECT COUNT(*) FROM information_schema.statistics
  WHERE table_schema = DATABASE()
    AND table_name = 'api_call_logs'
    AND index_name = 'idx_api_call_logs_created_at'
);
SET @api_call_logs_created_at_index_sql := IF(
  @api_call_logs_created_at_index_exists = 0,
  'CREATE INDEX idx_api_call_logs_created_at ON api_call_logs (created_at)',
  'SELECT 1'
);
PREPARE api_call_logs_created_at_index_stmt FROM @api_call_logs_created_at_index_sql;
EXECUTE api_call_logs_created_at_index_stmt;
DEALLOCATE PREPARE api_call_logs_created_at_index_stmt;

SET @api_call_logs_provider_created_at_index_exists := (
  SELECT COUNT(*) FROM information_schema.statistics
  WHERE table_schema = DATABASE()
    AND table_name = 'api_call_logs'
    AND index_name = 'idx_api_call_logs_provider_created_at'
);
SET @api_call_logs_provider_created_at_index_sql := IF(
  @api_call_logs_provider_created_at_index_exists = 0,
  'CREATE INDEX idx_api_call_logs_provider_created_at ON api_call_logs (provider, created_at)',
  'SELECT 1'
);
PREPARE api_call_logs_provider_created_at_index_stmt FROM @api_call_logs_provider_created_at_index_sql;
EXECUTE api_call_logs_provider_created_at_index_stmt;
DEALLOCATE PREPARE api_call_logs_provider_created_at_index_stmt;

SET @api_call_logs_act_created_at_index_exists := (
  SELECT COUNT(*) FROM information_schema.statistics
  WHERE table_schema = DATABASE()
    AND table_name = 'api_call_logs'
    AND index_name = 'idx_api_call_logs_act_created_at'
);
SET @api_call_logs_act_created_at_index_sql := IF(
  @api_call_logs_act_created_at_index_exists = 0,
  'CREATE INDEX idx_api_call_logs_act_created_at ON api_call_logs (act, created_at)',
  'SELECT 1'
);
PREPARE api_call_logs_act_created_at_index_stmt FROM @api_call_logs_act_created_at_index_sql;
EXECUTE api_call_logs_act_created_at_index_stmt;
DEALLOCATE PREPARE api_call_logs_act_created_at_index_stmt;
