CREATE TABLE IF NOT EXISTS "telephony_extensions" (
    "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
    "tenant_id" bigint NOT NULL,
    "target_type" text NOT NULL,
    "target_user_id" uuid,
    "target_team_id" bigint,
    "target_branch_id" bigint,
    "extension" text NOT NULL,
    "display_name" text,
    "sip_username" text,
    "sip_password" text,
    "enabled" boolean DEFAULT true NOT NULL,
    "created_at" timestamp with time zone DEFAULT now() NOT NULL,
    "updated_at" timestamp with time zone,
    "created_by" uuid,
    "updated_by" uuid
);

ALTER TABLE "telephony_trunks"
    ADD COLUMN IF NOT EXISTS "default_route_extension_id" uuid;

DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM pg_constraint WHERE conname = 'telephony_extensions_tenant_id_tenants_id_fk'
    ) THEN
        ALTER TABLE "telephony_extensions"
            ADD CONSTRAINT "telephony_extensions_tenant_id_tenants_id_fk"
            FOREIGN KEY ("tenant_id") REFERENCES "public"."tenants"("id")
            ON DELETE cascade ON UPDATE no action;
    END IF;

    IF NOT EXISTS (
        SELECT 1 FROM pg_constraint WHERE conname = 'telephony_extensions_target_user_id_auth_users_id_fk'
    ) THEN
        ALTER TABLE "telephony_extensions"
            ADD CONSTRAINT "telephony_extensions_target_user_id_auth_users_id_fk"
            FOREIGN KEY ("target_user_id") REFERENCES "public"."auth_users"("id")
            ON DELETE cascade ON UPDATE no action;
    END IF;

    IF NOT EXISTS (
        SELECT 1 FROM pg_constraint WHERE conname = 'telephony_extensions_target_team_id_teams_id_fk'
    ) THEN
        ALTER TABLE "telephony_extensions"
            ADD CONSTRAINT "telephony_extensions_target_team_id_teams_id_fk"
            FOREIGN KEY ("target_team_id") REFERENCES "public"."teams"("id")
            ON DELETE cascade ON UPDATE no action;
    END IF;

    IF NOT EXISTS (
        SELECT 1 FROM pg_constraint WHERE conname = 'telephony_extensions_target_branch_id_branches_id_fk'
    ) THEN
        ALTER TABLE "telephony_extensions"
            ADD CONSTRAINT "telephony_extensions_target_branch_id_branches_id_fk"
            FOREIGN KEY ("target_branch_id") REFERENCES "public"."branches"("id")
            ON DELETE cascade ON UPDATE no action;
    END IF;

    IF NOT EXISTS (
        SELECT 1 FROM pg_constraint WHERE conname = 'telephony_extensions_created_by_auth_users_id_fk'
    ) THEN
        ALTER TABLE "telephony_extensions"
            ADD CONSTRAINT "telephony_extensions_created_by_auth_users_id_fk"
            FOREIGN KEY ("created_by") REFERENCES "public"."auth_users"("id")
            ON DELETE no action ON UPDATE no action;
    END IF;

    IF NOT EXISTS (
        SELECT 1 FROM pg_constraint WHERE conname = 'telephony_extensions_updated_by_auth_users_id_fk'
    ) THEN
        ALTER TABLE "telephony_extensions"
            ADD CONSTRAINT "telephony_extensions_updated_by_auth_users_id_fk"
            FOREIGN KEY ("updated_by") REFERENCES "public"."auth_users"("id")
            ON DELETE no action ON UPDATE no action;
    END IF;

    IF NOT EXISTS (
        SELECT 1 FROM pg_constraint WHERE conname = 'telephony_trunks_default_route_extension_id_telephony_extensions_id_fk'
    ) THEN
        ALTER TABLE "telephony_trunks"
            ADD CONSTRAINT "telephony_trunks_default_route_extension_id_telephony_extensions_id_fk"
            FOREIGN KEY ("default_route_extension_id") REFERENCES "public"."telephony_extensions"("id")
            ON DELETE set null ON UPDATE no action;
    END IF;
END $$;

CREATE UNIQUE INDEX IF NOT EXISTS "telephony_extensions_tenant_extension_idx"
    ON "telephony_extensions" USING btree ("tenant_id", "extension");

CREATE INDEX IF NOT EXISTS "telephony_extensions_tenant_target_idx"
    ON "telephony_extensions" USING btree ("tenant_id", "target_type");
