-- 镭射卡 v3 表/字段/索引/约束备注（由 migrate_laser_card_v3_tables.sql 引用，亦可单独执行）

COMMENT ON TABLE public.laser_card_templates IS
    '镭射卡预设模板表。内置 5 套预设（dream/classic/holoFull/ice/sunset），与前端 laserPresets.js 对齐；本期只读种子，无管理端 CRUD。';

COMMENT ON COLUMN public.laser_card_templates.id IS '主键，自增';
COMMENT ON COLUMN public.laser_card_templates.template_code IS '业务编码，与五图 presetId 一致：dream | classic | holoFull | ice | sunset';
COMMENT ON COLUMN public.laser_card_templates.name IS 'C 端展示名称，如「梦幻」「全息」';
COMMENT ON COLUMN public.laser_card_templates.description IS '模板描述，可选';
COMMENT ON COLUMN public.laser_card_templates.status IS '发布状态：published（可用）| archived（下线）';
COMMENT ON COLUMN public.laser_card_templates.version IS '模板版本号；与 template_code 联合唯一（uk_lct_code_version）';
COMMENT ON COLUMN public.laser_card_templates.thumbnail_oss_key IS '列表缩略图 OSS 对象键，可选';
COMMENT ON COLUMN public.laser_card_templates.backdrop_options IS '底纹选项 JSON 数组，元素含 id/label/oss_key，如 liquidBlue → static/laser-bg/laser-bg-1.png';
COMMENT ON COLUMN public.laser_card_templates.render_config IS '默认渲染参数 JSON：style/angle/beam/backdrop/laserStrength 等，建议 < 16KB';
COMMENT ON COLUMN public.laser_card_templates.engine_min_version IS '最低客户端合成器版本，默认 compositor-1.0.0';
COMMENT ON COLUMN public.laser_card_templates.sort_order IS '模板列表排序，越小越靠前';
COMMENT ON COLUMN public.laser_card_templates.star_id IS '所属明星/星球 ID；0 表示全站通用模板';
COMMENT ON COLUMN public.laser_card_templates.created_by IS '创建人用户 ID；种子数据填 0';
COMMENT ON COLUMN public.laser_card_templates.created_at IS '创建时间，毫秒时间戳';
COMMENT ON COLUMN public.laser_card_templates.updated_at IS '更新时间，毫秒时间戳';
COMMENT ON COLUMN public.laser_card_templates.deleted_at IS '软删除时间，毫秒时间戳；NULL 表示未删除';

COMMENT ON INDEX public.uk_lct_code_version IS '同 template_code + version 在未删除行内唯一';
COMMENT ON INDEX public.idx_lct_status_star IS '按发布状态与 star_id 筛选模板列表';

COMMENT ON TABLE public.laser_card_instances IS
    '镭射卡业务实例表。一次「上传 → 五图选卡 → 铸造」对应一行；铸造前素材存 materials_snapshot，成功后回填 asset_id、mint_order_id。品类/工艺不写本表，见 assets.tags（cast:star_card + craft:laser）。';

COMMENT ON COLUMN public.laser_card_instances.id IS '主键，自增；对内使用';
COMMENT ON COLUMN public.laser_card_instances.instance_no IS '业务单号，UK；格式 LC + yyyyMMddHHmmss + 6 位随机';
COMMENT ON COLUMN public.laser_card_instances.instance_ulid IS '对外实例 ID，UK；如 lc_inst_01HXYZ...，API 路径参数';
COMMENT ON COLUMN public.laser_card_instances.template_id IS 'FK → laser_card_templates.id，用户选中的预设';
COMMENT ON COLUMN public.laser_card_instances.template_code IS '模板编码冗余，避免列表 JOIN templates';
COMMENT ON COLUMN public.laser_card_instances.template_version IS '创建实例时锁定的模板版本快照';
COMMENT ON COLUMN public.laser_card_instances.owner_user_id IS '持有者用户 ID；鉴权须等于 JWT user_id';
COMMENT ON COLUMN public.laser_card_instances.star_id IS '多星隔离：所属明星/星球 ID（非「星卡」品类）';
COMMENT ON COLUMN public.laser_card_instances.status IS '状态机：rendered（已选卡未铸造）| minting（铸造中）| minted（已完成）';
COMMENT ON COLUMN public.laser_card_instances.client_request_id IS '客户端幂等 UUID；与 owner_user_id 联合唯一（uk_lci_user_client_req）';
COMMENT ON COLUMN public.laser_card_instances.render_config IS '用户最终渲染参数 JSON 快照，可选';
COMMENT ON COLUMN public.laser_card_instances.materials_snapshot IS
    '铸造前素材 OSS 清单 JSON 数组（3–4 项，无抠图则无 cutout）。每项字段：role(source|cutout|backdrop|composite)、oss_key、hash、width、height、mime_type、file_size、preset_id(仅 backdrop/composite)。不存 materials.id；铸造后写入 asset_material_relations';
COMMENT ON COLUMN public.laser_card_instances.composite_oss_key IS '用户选中的合成图 OSS key，通常等于 snapshot 中 composite 项';
COMMENT ON COLUMN public.laser_card_instances.composite_material_id IS '铸造成功后回填 materials.id（对应 asset_material_relations.main 层）';
COMMENT ON COLUMN public.laser_card_instances.asset_id IS 'FK → assets.id；铸造成功后回填，铸造前为 NULL';
COMMENT ON COLUMN public.laser_card_instances.mint_order_id IS 'FK → mint_orders.order_id；发起铸造时关联';
COMMENT ON COLUMN public.laser_card_instances.idempotency_key IS '最后一次写操作的幂等键，可选';
COMMENT ON COLUMN public.laser_card_instances.version IS '乐观锁版本号，每次更新实例 +1';
COMMENT ON COLUMN public.laser_card_instances.created_at IS '创建时间，毫秒时间戳';
COMMENT ON COLUMN public.laser_card_instances.updated_at IS '更新时间，毫秒时间戳';
COMMENT ON COLUMN public.laser_card_instances.deleted_at IS '软删除时间，毫秒时间戳；NULL 表示未删除';

COMMENT ON INDEX public.uk_lci_instance_no IS '业务单号唯一';
COMMENT ON INDEX public.uk_lci_instance_ulid IS '对外实例 ID 唯一';
COMMENT ON INDEX public.uk_lci_user_client_req IS '同一用户下 client_request_id 幂等（未删除且非空）';
COMMENT ON INDEX public.idx_lci_owner_status IS '按用户与状态查实例列表';
COMMENT ON INDEX public.idx_lci_asset IS '按已铸造资产反查实例';
COMMENT ON INDEX public.idx_lci_star_created IS '按星球与时间倒序列表';

COMMENT ON TABLE public.laser_card_operation_logs IS
    '镭射卡操作审计流水表。只追加不更新；记录 create_instance / mint_start / mint_success / mint_fail 等；payload_json 禁止存大图或 base64。';

COMMENT ON COLUMN public.laser_card_operation_logs.id IS '主键，自增';
COMMENT ON COLUMN public.laser_card_operation_logs.instance_id IS 'FK → laser_card_instances.id；实例删除时级联删除日志';
COMMENT ON COLUMN public.laser_card_operation_logs.instance_no IS '实例业务单号冗余，便于按单号检索';
COMMENT ON COLUMN public.laser_card_operation_logs.operator_user_id IS '操作人用户 ID';
COMMENT ON COLUMN public.laser_card_operation_logs.action IS '动作：create_instance | generate_variants | mint_start | mint_success | mint_fail';
COMMENT ON COLUMN public.laser_card_operation_logs.status_before IS '变更前实例 status，可选';
COMMENT ON COLUMN public.laser_card_operation_logs.status_after IS '变更后实例 status，可选';
COMMENT ON COLUMN public.laser_card_operation_logs.request_id IS '请求追踪 ID，可选';
COMMENT ON COLUMN public.laser_card_operation_logs.payload_json IS '请求摘要 JSON，禁止存图片二进制或 base64';
COMMENT ON COLUMN public.laser_card_operation_logs.result_json IS '响应摘要 JSON，可选';
COMMENT ON COLUMN public.laser_card_operation_logs.ip_address IS '客户端 IP，IPv4/IPv6';
COMMENT ON COLUMN public.laser_card_operation_logs.user_agent IS '客户端 User-Agent';
COMMENT ON COLUMN public.laser_card_operation_logs.latency_ms IS '接口耗时（毫秒），可选';
COMMENT ON COLUMN public.laser_card_operation_logs.err_code IS '失败时的业务错误码，可选';
COMMENT ON COLUMN public.laser_card_operation_logs.created_at IS '日志创建时间，毫秒时间戳';

COMMENT ON INDEX public.idx_lclog_instance_time IS '按实例查操作历史（时间倒序）';
COMMENT ON INDEX public.idx_lclog_operator_time IS '按操作人查历史';
COMMENT ON INDEX public.idx_lclog_action_time IS '按动作类型统计与排查';

COMMENT ON CONSTRAINT fk_lci_template ON public.laser_card_instances IS
    '关联所选镭射预设模板';
COMMENT ON CONSTRAINT fk_lci_asset ON public.laser_card_instances IS
    '铸造成功后的藏品 assets.id；删除资产时置 NULL';
COMMENT ON CONSTRAINT fk_lci_mint_order ON public.laser_card_instances IS
    '关联铸造订单 mint_orders.order_id';
COMMENT ON CONSTRAINT fk_lci_composite_material ON public.laser_card_instances IS
    '选中合成图在 materials 表中的主键（main 层）';
COMMENT ON CONSTRAINT chk_lci_status ON public.laser_card_instances IS
    '实例状态枚举：rendered | minting | minted';

COMMENT ON CONSTRAINT fk_lclog_instance ON public.laser_card_operation_logs IS
    '关联镭射实例；实例删除时级联删除审计日志';
