-- =====================================================================================
-- Schema: 医养执行端 Delivery 基础表升级
-- 位置：docs/sql/10_schema/delivery/ak_delivery_system_v2.sql
-- 对象类型：ALTER TABLE / INDEX / TRIGGER
-- 版本：v2
-- 说明：在 v1 基础上最小升级为“医养上门服务执行端”可用模型。
--       补齐 uid 唯一索引、软删除、机构关联、staff_no、在线状态、资质状态。
-- =====================================================================================

-- 0. 通用 updated_at 触发器
CREATE OR REPLACE FUNCTION public.tg_set_updated_at()
RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
  NEW.updated_at = now();
  RETURN NEW;
END;
$$;

-- 1. 配送/执行人员表升级
ALTER TABLE public.ml_delivery_staff
  ADD COLUMN IF NOT EXISTS station_id UUID REFERENCES public.ml_delivery_stations(id) ON DELETE SET NULL,
  ADD COLUMN IF NOT EXISTS staff_no TEXT,
  ADD COLUMN IF NOT EXISTS online_status TEXT NOT NULL DEFAULT 'resting',
  ADD COLUMN IF NOT EXISTS certificate_status TEXT NOT NULL DEFAULT 'pending',
  ADD COLUMN IF NOT EXISTS certificate_expire_at DATE,
  ADD COLUMN IF NOT EXISTS service_area TEXT NOT NULL DEFAULT '',
  ADD COLUMN IF NOT EXISTS skills JSONB NOT NULL DEFAULT '[]'::jsonb,
  ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ,
  ADD COLUMN IF NOT EXISTS deleted_by UUID REFERENCES public.ak_users(id) ON DELETE SET NULL;

ALTER TABLE public.ml_delivery_staff
  DROP CONSTRAINT IF EXISTS chk_ml_delivery_staff_online_status;
ALTER TABLE public.ml_delivery_staff
  ADD CONSTRAINT chk_ml_delivery_staff_online_status
  CHECK (online_status IN ('online', 'resting', 'busy'));

ALTER TABLE public.ml_delivery_staff
  DROP CONSTRAINT IF EXISTS chk_ml_delivery_staff_certificate_status;
ALTER TABLE public.ml_delivery_staff
  ADD CONSTRAINT chk_ml_delivery_staff_certificate_status
  CHECK (certificate_status IN ('valid', 'expired', 'pending'));

-- 2. 站点/机构表升级
ALTER TABLE public.ml_delivery_stations
  ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ,
  ADD COLUMN IF NOT EXISTS deleted_by UUID REFERENCES public.ak_users(id) ON DELETE SET NULL;

-- 3. 索引
CREATE UNIQUE INDEX IF NOT EXISTS uq_ml_delivery_staff_uid_active
  ON public.ml_delivery_staff(uid)
  WHERE uid IS NOT NULL AND deleted_at IS NULL;

CREATE UNIQUE INDEX IF NOT EXISTS uq_ml_delivery_staff_staff_no_active
  ON public.ml_delivery_staff(staff_no)
  WHERE staff_no IS NOT NULL AND deleted_at IS NULL;

CREATE INDEX IF NOT EXISTS idx_ml_delivery_staff_station_id
  ON public.ml_delivery_staff(station_id)
  WHERE deleted_at IS NULL;

CREATE INDEX IF NOT EXISTS idx_ml_delivery_staff_deleted_at
  ON public.ml_delivery_staff(deleted_at);

CREATE INDEX IF NOT EXISTS idx_ml_delivery_staff_status_active
  ON public.ml_delivery_staff(status, is_active)
  WHERE deleted_at IS NULL;

CREATE INDEX IF NOT EXISTS idx_ml_delivery_stations_deleted_at
  ON public.ml_delivery_stations(deleted_at);

CREATE INDEX IF NOT EXISTS idx_ml_delivery_stations_status_active
  ON public.ml_delivery_stations(status)
  WHERE deleted_at IS NULL;

-- 4. 自动维护 updated_at
DROP TRIGGER IF EXISTS trg_ml_delivery_staff_set_updated_at ON public.ml_delivery_staff;
CREATE TRIGGER trg_ml_delivery_staff_set_updated_at
BEFORE UPDATE ON public.ml_delivery_staff
FOR EACH ROW
EXECUTE FUNCTION public.tg_set_updated_at();

DROP TRIGGER IF EXISTS trg_ml_delivery_stations_set_updated_at ON public.ml_delivery_stations;
CREATE TRIGGER trg_ml_delivery_stations_set_updated_at
BEFORE UPDATE ON public.ml_delivery_stations
FOR EACH ROW
EXECUTE FUNCTION public.tg_set_updated_at();

-- 5. 注释
COMMENT ON COLUMN public.ml_delivery_staff.station_id IS '所属机构/服务站点 ID';
COMMENT ON COLUMN public.ml_delivery_staff.staff_no IS '服务人员编号';
COMMENT ON COLUMN public.ml_delivery_staff.online_status IS '在线状态：online/resting/busy';
COMMENT ON COLUMN public.ml_delivery_staff.certificate_status IS '资质状态：valid/expired/pending';
COMMENT ON COLUMN public.ml_delivery_staff.certificate_expire_at IS '主要资质到期日期';
COMMENT ON COLUMN public.ml_delivery_staff.service_area IS '服务区域描述';
COMMENT ON COLUMN public.ml_delivery_staff.skills IS '技能标签 JSON 数组';
COMMENT ON COLUMN public.ml_delivery_staff.deleted_at IS '软删除时间';
COMMENT ON COLUMN public.ml_delivery_staff.deleted_by IS '软删除操作人';
COMMENT ON COLUMN public.ml_delivery_stations.deleted_at IS '软删除时间';
COMMENT ON COLUMN public.ml_delivery_stations.deleted_by IS '软删除操作人';
