CREATE TABLE IF NOT EXISTS `op_ocpi_backends` (
  `backend_id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `url` varchar(1024) NOT NULL,
  `remote_versions_url` varchar(1024) DEFAULT NULL,
  `peer_versions_url` varchar(1024) DEFAULT NULL,
  `active_version` varchar(16) DEFAULT NULL,
  `token` text,
  `peer_token` text,
  `credentials_token` text,
  `modules` varchar(255) NOT NULL DEFAULT 'cdrs',
  `enabled` tinyint(1) NOT NULL DEFAULT 1,
  `last_credentials_status` varchar(255) DEFAULT NULL,
  `last_credentials_at` timestamp NULL DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`backend_id`),
  KEY `idx_op_ocpi_backends_enabled` (`enabled`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `op_ocpi_wallbox_backends` (
  `station_id` varchar(50) NOT NULL,
  `backend_id` int NOT NULL,
  `enabled` tinyint(1) NOT NULL DEFAULT 1,
  `priority` int NOT NULL DEFAULT 100,
  `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`station_id`, `backend_id`),
  KEY `idx_op_ocpi_wallbox_backends_backend_id` (`backend_id`),
  CONSTRAINT `fk_ocpi_wallbox_backend_backend` FOREIGN KEY (`backend_id`) REFERENCES `op_ocpi_backends` (`backend_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
