-- ============================================
-- 此文件由 Meoo Cloud 自动生成，请勿修改
-- This file is auto-generated by Meoo Cloud, DO NOT MODIFY
-- ============================================


-- 创建水洗流程步骤表
CREATE TABLE washing_process_steps (
 id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
 washing_plan_id UUID NOT NULL REFERENCES washing_plans(id) ON DELETE CASCADE,
 company_id UUID NOT NULL REFERENCES companies(id) ON DELETE CASCADE,
 
 -- 步骤类型: confirm(确认计划), start_washing(开始水洗), complete(拆布打匹出库)
 step_type TEXT NOT NULL CHECK (step_type IN ('confirm', 'start_washing', 'complete')),
 
 -- 状态: pending(待处理), active(进行中), completed(已完成)
 status TEXT NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'active', 'completed')),
 
 -- 时间戳
 timestamp TIMESTAMPTZ,
 
 -- 操作人
 operator_id UUID REFERENCES profiles(id),
 
 -- 备注
 notes TEXT,
 
 -- 元数据
 created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
 updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- 创建索引
CREATE INDEX idx_washing_steps_plan ON washing_process_steps(washing_plan_id);
CREATE INDEX idx_washing_steps_company ON washing_process_steps(company_id);
CREATE INDEX idx_washing_steps_type ON washing_process_steps(step_type);
CREATE INDEX idx_washing_steps_status ON washing_process_steps(status);

-- 启用 RLS
ALTER TABLE washing_process_steps ENABLE ROW LEVEL SECURITY;

-- RLS 策略
CREATE POLICY company_select_washing_steps ON washing_process_steps
 FOR SELECT USING (company_id = get_user_master_company_id());

CREATE POLICY company_insert_washing_steps ON washing_process_steps
 FOR INSERT WITH CHECK (company_id = get_user_master_company_id());

CREATE POLICY company_update_washing_steps ON washing_process_steps
 FOR UPDATE USING (company_id = get_user_master_company_id());

CREATE POLICY company_delete_washing_steps ON washing_process_steps
 FOR DELETE USING (company_id = get_user_master_company_id());

