-- Extend client_targets with period_type and work_days, rename weekly_hours to target_hours
-- This migration adds support for monthly targets and work-day-aware balance calculation

-- Rename weekly_hours to target_hours
ALTER TABLE "client_targets" RENAME COLUMN "weekly_hours" TO "target_hours";

-- Add period_type column with default 'weekly' for backwards compatibility
ALTER TABLE "client_targets" ADD COLUMN "period_type" VARCHAR(10) NOT NULL DEFAULT 'weekly';

-- Add work_days column with default Mon-Fri (1-5) for backwards compatibility
ALTER TABLE "client_targets" ADD COLUMN "work_days" INTEGER[] NOT NULL DEFAULT '{1,2,3,4,5}';

-- Add check constraint to ensure period_type is valid
ALTER TABLE "client_targets" ADD CONSTRAINT "period_type_check" CHECK ("period_type" IN ('weekly', 'monthly'));

-- Add check constraint to ensure at least one work day is selected
ALTER TABLE "client_targets" ADD CONSTRAINT "work_days_not_empty_check" CHECK (array_length("work_days", 1) > 0);
