CREATE DATABASE IF NOT EXISTS ota_server DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE ota_server;

CREATE TABLE IF NOT EXISTS ota_release (
    id BIGINT PRIMARY KEY AUTO_INCREMENT,
    release_version VARCHAR(128) NOT NULL UNIQUE,
    release_notes TEXT NOT NULL,
    upgrade_mode VARCHAR(32) NOT NULL,
    published_at DATETIME(6) NOT NULL,
    parking_required TINYINT(1) NOT NULL DEFAULT 0,
    components_json JSON NOT NULL
);

CREATE TABLE IF NOT EXISTS ota_vehicle (
    id BIGINT PRIMARY KEY AUTO_INCREMENT,
    vehicle_id VARCHAR(64) NOT NULL UNIQUE,
    vin VARCHAR(64) NOT NULL,
    current_release VARCHAR(128) NOT NULL,
    current_backend_version VARCHAR(128) NULL,
    current_frontend_version VARCHAR(128) NULL,
    current_ros_version VARCHAR(128) NULL,
    last_seen_at DATETIME(6) NULL,
    agent_status VARCHAR(32) NULL,
    target_release VARCHAR(128) NULL,
    last_result VARCHAR(512) NULL,
    images_json JSON NULL,
    backup_file VARCHAR(512) NULL
);

CREATE TABLE IF NOT EXISTS ota_vehicle_assignment (
    id BIGINT PRIMARY KEY AUTO_INCREMENT,
    vehicle_id VARCHAR(64) NOT NULL UNIQUE,
    release_version VARCHAR(128) NOT NULL,
    task_status VARCHAR(32) NOT NULL,
    prompted_at DATETIME(6) NULL,
    confirmed_at DATETIME(6) NULL,
    started_at DATETIME(6) NULL,
    finished_at DATETIME(6) NULL,
    postpone_count INT NOT NULL DEFAULT 0,
    last_message VARCHAR(512) NULL,
    CONSTRAINT fk_assignment_release_version FOREIGN KEY (release_version) REFERENCES ota_release(release_version)
);

CREATE TABLE IF NOT EXISTS sys_user (
    id BIGINT PRIMARY KEY AUTO_INCREMENT,
    username VARCHAR(64) NOT NULL UNIQUE,
    password VARCHAR(128) NOT NULL,
    nickname VARCHAR(64) NOT NULL,
    status TINYINT NOT NULL DEFAULT 1,
    created_at DATETIME(6) NOT NULL,
    updated_at DATETIME(6) NOT NULL
);

CREATE TABLE IF NOT EXISTS sys_role (
    id BIGINT PRIMARY KEY AUTO_INCREMENT,
    role_code VARCHAR(64) NOT NULL UNIQUE,
    role_name VARCHAR(64) NOT NULL,
    status TINYINT NOT NULL DEFAULT 1,
    created_at DATETIME(6) NOT NULL,
    updated_at DATETIME(6) NOT NULL
);

CREATE TABLE IF NOT EXISTS sys_user_role (
    id BIGINT PRIMARY KEY AUTO_INCREMENT,
    user_id BIGINT NOT NULL,
    role_id BIGINT NOT NULL,
    created_at DATETIME(6) NOT NULL,
    UNIQUE KEY uk_user_role (user_id, role_id),
    CONSTRAINT fk_user_role_user FOREIGN KEY (user_id) REFERENCES sys_user(id),
    CONSTRAINT fk_user_role_role FOREIGN KEY (role_id) REFERENCES sys_role(id)
);

INSERT INTO sys_role (id, role_code, role_name, status, created_at, updated_at)
VALUES
    (1, 'SUPER_ADMIN', '系统管理员', 1, NOW(6), NOW(6)),
    (2, 'ADMIN', '运维管理员', 1, NOW(6), NOW(6)),
    (3, 'AGENT', '车端代理', 1, NOW(6), NOW(6))
ON DUPLICATE KEY UPDATE role_name = VALUES(role_name), status = VALUES(status), updated_at = NOW(6);

INSERT INTO sys_user (id, username, password, nickname, status, created_at, updated_at)
VALUES
    (1, 'admin', 'e10adc3949ba59abbe56e057f20f883e', '超级管理员', 1, NOW(6), NOW(6)),
    (2, 'agent', 'e10adc3949ba59abbe56e057f20f883e', '车端代理', 1, NOW(6), NOW(6))
ON DUPLICATE KEY UPDATE password = VALUES(password), nickname = VALUES(nickname), status = VALUES(status), updated_at = NOW(6);

INSERT INTO sys_user_role (user_id, role_id, created_at)
VALUES
    (1, 1, NOW(6)),
    (2, 3, NOW(6))
ON DUPLICATE KEY UPDATE created_at = VALUES(created_at);
