create table if not exists model_runtime_deployments (
    id varchar(64) primary key,
    model_group_id varchar(64) not null references model_groups(id),
    model_version_id varchar(64) not null references model_versions(id),
    name varchar(128) not null,
    environment varchar(32) not null,
    service_url text not null,
    health_path varchar(256) not null default '/health',
    runtime_path varchar(256) not null default '/api/v1/runtime',
    inference_path varchar(256) not null,
    test_path varchar(256) not null,
    runtime_engine varchar(64) not null,
    execution_provider varchar(96) not null,
    device varchar(64) not null,
    precision varchar(16) not null,
    artifact_uri text,
    auth_secret_ref varchar(256),
    replicas int not null default 1,
    max_concurrency int not null default 1,
    request_timeout_seconds int not null default 60,
    config jsonb not null default '{}'::jsonb,
    status varchar(32) not null default 'ready',
    health_status varchar(32) not null default 'unknown',
    health_message text,
    health_snapshot jsonb not null default '{}'::jsonb,
    last_health_at timestamptz,
    active boolean not null default false,
    revision int not null default 1,
    created_by varchar(128) not null,
    updated_by varchar(128) not null,
    created_at timestamptz not null default now(),
    updated_at timestamptz not null default now(),
    unique(model_group_id, name)
);

create unique index if not exists uk_model_runtime_active_group
    on model_runtime_deployments(model_group_id) where active=true;

create index if not exists idx_model_runtime_deployments_group
    on model_runtime_deployments(model_group_id, active desc, updated_at desc);

insert into model_runtime_deployments(
    id, model_group_id, model_version_id, name, environment, service_url,
    inference_path, test_path, runtime_engine, execution_provider, device,
    precision, artifact_uri, replicas, max_concurrency, request_timeout_seconds,
    config, status, health_status, active, created_by, updated_by
) values
('runtime-vision-detector-cpu', 'group-vision-detector', 'model-vision-detector-cpu100', '本地 CPU 调试', 'local', 'http://vision-inference:8101',
 '/api/v1/inference/detect', '/api/v1/inference/test', 'onnxruntime', 'CPUExecutionProvider', 'cpu', 'FP32', 'file:///models/vision-detector/ppyoloe-sod-s/model.onnx', 1, 1, 60,
 '{"expected_profile":"cpu-local","allow_fallback":true,"batch_size":1}', 'active', 'unknown', true, '系统初始化', '系统初始化'),
('runtime-vision-segmenter-cpu', 'group-vision-segmenter', 'model-vision-segmenter-cpu100', '本地 CPU 调试', 'local', 'http://vision-inference:8101',
 '/api/v1/inference/detect', '/api/v1/inference/test', 'onnxruntime', 'CPUExecutionProvider', 'cpu', 'FP32', 'file:///models/vision-segmenter/pp-liteseg-stdc1/model.onnx', 1, 1, 60,
 '{"expected_profile":"cpu-local","allow_fallback":true,"batch_size":1}', 'active', 'unknown', true, '系统初始化', '系统初始化'),
('runtime-thermal-cpu', 'group-thermal-analyzer', 'model-thermal-cpu100', '本地 CPU 调试', 'local', 'http://vision-inference:8101',
 '/api/v1/inference/detect', '/api/v1/inference/test', 'onnxruntime', 'CPUExecutionProvider', 'cpu', 'FP32', 'file:///models/thermal-analyzer/ppyoloe-s/model.onnx', 1, 1, 60,
 '{"expected_profile":"cpu-local","allow_fallback":true,"batch_size":1}', 'active', 'unknown', true, '系统初始化', '系统初始化'),
('runtime-change-cpu', 'group-change-detector', 'model-change-cpu100', '本地 CPU 调试', 'local', 'http://pointcloud-analysis:8102',
 '/api/v1/pointcloud/analyze', '/api/v1/pointcloud/test', 'onnxruntime', 'CPUExecutionProvider', 'cpu', 'FP32', 'file:///models/change-detector/bit-r18/model.onnx', 1, 1, 120,
 '{"expected_profile":"cpu-local","allow_fallback":true,"batch_size":1}', 'active', 'unknown', true, '系统初始化', '系统初始化'),
('runtime-pointcloud-cpu', 'group-pointcloud-analyzer', 'model-pointcloud-cpu100', '本地 CPU 调试', 'local', 'http://pointcloud-analysis:8102',
 '/api/v1/pointcloud/analyze', '/api/v1/pointcloud/test', 'open3d', 'CPU', 'cpu', 'FP64', 'builtin://open3d-geometry', 1, 1, 180,
 '{"expected_profile":"cpu-local","allow_fallback":false,"max_point_count":500000}', 'active', 'unknown', true, '系统初始化', '系统初始化')
on conflict (id) do nothing;

insert into model_runtime_deployments(
    id, model_group_id, model_version_id, name, environment, service_url,
    inference_path, test_path, runtime_engine, execution_provider, device,
    precision, artifact_uri, replicas, max_concurrency, request_timeout_seconds,
    config, status, health_status, active, created_by, updated_by
) values
('runtime-vision-detector-gpu', 'group-vision-detector', 'model-vision-detector-v100', '服务器 GPU 生产', 'production', 'http://vision-inference:8101',
 '/api/v1/inference/detect', '/api/v1/inference/test', 'onnxruntime-gpu', 'CUDAExecutionProvider', 'cuda:0', 'FP16', 'file:///models/vision-detector/v1.0.0/model.onnx', 2, 8, 30,
 '{"expected_profile":"server-gpu","allow_fallback":false,"batch_size":4}', 'ready', 'unknown', false, '系统初始化', '系统初始化'),
('runtime-vision-segmenter-gpu', 'group-vision-segmenter', 'model-vision-segmenter-v100', '服务器 GPU 生产', 'production', 'http://vision-inference:8101',
 '/api/v1/inference/detect', '/api/v1/inference/test', 'onnxruntime-gpu', 'CUDAExecutionProvider', 'cuda:0', 'FP16', 'file:///models/vision-segmenter/v1.0.0/model.onnx', 2, 6, 45,
 '{"expected_profile":"server-gpu","allow_fallback":false,"batch_size":2}', 'ready', 'unknown', false, '系统初始化', '系统初始化'),
('runtime-thermal-gpu', 'group-thermal-analyzer', 'model-thermal-v100', '服务器 GPU 生产', 'production', 'http://vision-inference:8101',
 '/api/v1/inference/detect', '/api/v1/inference/test', 'onnxruntime-gpu', 'CUDAExecutionProvider', 'cuda:1', 'FP16', 'file:///models/thermal-analyzer/v1.0.0/model.onnx', 1, 4, 45,
 '{"expected_profile":"server-gpu","allow_fallback":false,"batch_size":2}', 'ready', 'unknown', false, '系统初始化', '系统初始化'),
('runtime-change-gpu', 'group-change-detector', 'model-change-v100', '服务器 GPU 生产', 'production', 'http://pointcloud-analysis:8102',
 '/api/v1/pointcloud/analyze', '/api/v1/pointcloud/test', 'onnxruntime-gpu', 'CUDAExecutionProvider', 'cuda:1', 'FP16', 'file:///models/change-detector/v1.0.0/model.onnx', 2, 4, 90,
 '{"expected_profile":"server-gpu","allow_fallback":false,"batch_size":2}', 'ready', 'unknown', false, '系统初始化', '系统初始化'),
('runtime-pointcloud-server', 'group-pointcloud-analyzer', 'model-pointcloud-v100', '服务器点云生产', 'production', 'http://pointcloud-analysis:8102',
 '/api/v1/pointcloud/analyze', '/api/v1/pointcloud/test', 'open3d', 'CPU', 'cpu:server-pool', 'FP64', 'builtin://open3d-geometry', 1, 3, 300,
 '{"expected_profile":"server-gpu","allow_fallback":false,"max_point_count":5000000}', 'ready', 'unknown', false, '系统初始化', '系统初始化')
on conflict (id) do nothing;
