-- Add uniform_date (if missing), chart_data, and unique constraint for uniformity records
ALTER TABLE project_flock_kandang_uniformity
    ADD COLUMN IF NOT EXISTS uniform_date TIMESTAMPTZ,
    ADD COLUMN IF NOT EXISTS chart_data JSONB;

DO $$
BEGIN
    IF EXISTS (
        SELECT 1
        FROM information_schema.columns
        WHERE table_schema = 'public'
          AND table_name = 'project_flock_kandang_uniformity'
          AND column_name = 'deleted_at'
    ) THEN
        CREATE UNIQUE INDEX IF NOT EXISTS idx_project_flock_kandang_uniformity_unique
            ON project_flock_kandang_uniformity (project_flock_kandang_id, week, uniform_date)
            WHERE deleted_at IS NULL;
    ELSE
        CREATE UNIQUE INDEX IF NOT EXISTS idx_project_flock_kandang_uniformity_unique
            ON project_flock_kandang_uniformity (project_flock_kandang_id, week, uniform_date);
    END IF;
END $$;

CREATE INDEX IF NOT EXISTS idx_project_flock_kandang_uniformity_uniform_date
    ON project_flock_kandang_uniformity (uniform_date);
