ALTER TABLE "crm_customers"
ADD COLUMN IF NOT EXISTS "owner_user_id" text,
ADD COLUMN IF NOT EXISTS "owner_assigned_at" timestamp with time zone,
ADD COLUMN IF NOT EXISTS "owner_assigned_by" text;

CREATE TABLE IF NOT EXISTS "crm_customer_owner_history" (
  "id" text PRIMARY KEY NOT NULL,
  "organization_id" text NOT NULL,
  "customer_id" text NOT NULL,
  "old_owner_user_id" text,
  "new_owner_user_id" text,
  "changed_by" text NOT NULL,
  "changed_at" timestamp with time zone DEFAULT now() NOT NULL,
  "remark" text,
  "created_at" timestamp with time zone DEFAULT now() NOT NULL,
  "updated_at" timestamp with time zone DEFAULT now() NOT NULL,
  "deleted_at" timestamp with time zone
);

CREATE TABLE IF NOT EXISTS "crm_contact_shares" (
  "id" text PRIMARY KEY NOT NULL,
  "organization_id" text NOT NULL,
  "contact_id" text NOT NULL,
  "shared_to_user_id" text NOT NULL,
  "shared_by_user_id" text NOT NULL,
  "shared_at" timestamp with time zone DEFAULT now() NOT NULL,
  "is_active" boolean DEFAULT true NOT NULL,
  "remark" text,
  "created_at" timestamp with time zone DEFAULT now() NOT NULL,
  "updated_at" timestamp with time zone DEFAULT now() NOT NULL,
  "deleted_at" timestamp with time zone
);

CREATE UNIQUE INDEX IF NOT EXISTS "crm_contact_shares_org_contact_shared_user_idx"
ON "crm_contact_shares" ("organization_id", "contact_id", "shared_to_user_id");
