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


-- 自定义枚举类型
CREATE TYPE public.app_role AS ENUM ('purchaser', 'textile', 'washing');
CREATE TYPE public.factory_type AS ENUM ('textile', 'washing');
CREATE TYPE public.plan_status AS ENUM ('pending', 'producing', 'completed');
CREATE TYPE public.step_type AS ENUM ('confirm', 'yarn_purchase', 'dyeing', 'machine_start', 'fabric_warehouse');
CREATE TYPE public.step_status AS ENUM ('pending', 'active', 'completed');
CREATE TYPE public.warehouse_type AS ENUM ('raw_fabric', 'fabric', 'finished', 'yarn');
CREATE TYPE public.payment_status AS ENUM ('pending', 'completed');

-- 公司表
CREATE TABLE public.companies (
 id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
 name TEXT NOT NULL,
 role public.app_role NOT NULL,
 address TEXT,
 contact_phone TEXT,
 created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- 用户配置表
CREATE TABLE public.profiles (
 id UUID PRIMARY KEY REFERENCES auth.users(id) ON DELETE CASCADE,
 username TEXT UNIQUE NOT NULL,
 phone TEXT,
 company_id UUID REFERENCES public.companies(id) ON DELETE SET NULL,
 is_master BOOLEAN NOT NULL DEFAULT true,
 master_id UUID REFERENCES auth.users(id) ON DELETE SET NULL,
 created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- 公司成员表
CREATE TABLE public.company_members (
 id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
 company_id UUID NOT NULL REFERENCES public.companies(id) ON DELETE CASCADE,
 user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
 role TEXT NOT NULL DEFAULT 'member',
 created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
 UNIQUE(company_id, user_id)
);

