-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "public";

-- CreateTable
CREATE TABLE "public"."analytics" (
    "id" VARCHAR NOT NULL DEFAULT gen_random_uuid(),
    "channel_id" VARCHAR,
    "date" TIMESTAMP(6) NOT NULL,
    "messages_sent" INTEGER DEFAULT 0,
    "messages_delivered" INTEGER DEFAULT 0,
    "messages_read" INTEGER DEFAULT 0,
    "messages_replied" INTEGER DEFAULT 0,
    "new_contacts" INTEGER DEFAULT 0,
    "active_campaigns" INTEGER DEFAULT 0,
    "created_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "analytics_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "public"."api_logs" (
    "id" VARCHAR NOT NULL DEFAULT gen_random_uuid(),
    "channel_id" VARCHAR,
    "request_type" VARCHAR(50) NOT NULL,
    "endpoint" TEXT NOT NULL,
    "method" VARCHAR(10) NOT NULL,
    "request_body" JSONB,
    "response_status" INTEGER,
    "response_body" JSONB,
    "duration" INTEGER,
    "created_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "api_logs_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "public"."automation_edges" (
    "id" VARCHAR NOT NULL,
    "automation_id" VARCHAR NOT NULL,
    "source_node_id" VARCHAR NOT NULL,
    "target_node_id" VARCHAR NOT NULL,
    "animated" BOOLEAN DEFAULT false,
    "created_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "automation_edges_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "public"."automation_execution_logs" (
    "id" VARCHAR NOT NULL DEFAULT gen_random_uuid(),
    "execution_id" VARCHAR NOT NULL,
    "node_id" VARCHAR NOT NULL,
    "node_type" TEXT NOT NULL,
    "status" TEXT NOT NULL,
    "input" JSONB DEFAULT '{}',
    "output" JSONB DEFAULT '{}',
    "error" TEXT,
    "executed_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "automation_execution_logs_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "public"."automation_executions" (
    "id" VARCHAR NOT NULL DEFAULT gen_random_uuid(),
    "automation_id" VARCHAR NOT NULL,
    "contact_id" VARCHAR,
    "conversation_id" VARCHAR,
    "trigger_data" JSONB DEFAULT '{}',
    "status" TEXT NOT NULL,
    "current_node_id" VARCHAR,
    "execution_path" JSONB DEFAULT '[]',
    "variables" JSONB DEFAULT '{}',
    "error" TEXT,
    "started_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,
    "completed_at" TIMESTAMP(6),
    "result" TEXT,

    CONSTRAINT "automation_executions_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "public"."automation_nodes" (
    "id" VARCHAR NOT NULL DEFAULT gen_random_uuid(),
    "automation_id" VARCHAR NOT NULL,
    "node_id" VARCHAR NOT NULL,
    "type" TEXT NOT NULL,
    "subtype" TEXT,
    "position" JSONB DEFAULT '{}',
    "data" JSONB DEFAULT '{}',
    "connections" JSONB DEFAULT '[]',
    "created_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,
    "measured" JSONB DEFAULT '{}',

    CONSTRAINT "automation_nodes_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "public"."automations" (
    "id" VARCHAR NOT NULL DEFAULT gen_random_uuid(),
    "channel_id" VARCHAR,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "trigger" TEXT NOT NULL,
    "trigger_config" JSONB DEFAULT '{}',
    "status" TEXT DEFAULT 'inactive',
    "execution_count" INTEGER DEFAULT 0,
    "last_executed_at" TIMESTAMP(6),
    "created_by" VARCHAR,
    "created_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "automations_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "public"."campaign_recipients" (
    "id" VARCHAR NOT NULL DEFAULT gen_random_uuid(),
    "campaign_id" VARCHAR NOT NULL,
    "contact_id" VARCHAR,
    "phone" TEXT NOT NULL,
    "name" TEXT,
    "status" TEXT DEFAULT 'pending',
    "whatsapp_message_id" VARCHAR,
    "template_params" JSONB DEFAULT '{}',
    "sent_at" TIMESTAMP(6),
    "delivered_at" TIMESTAMP(6),
    "read_at" TIMESTAMP(6),
    "error_code" VARCHAR,
    "error_message" TEXT,
    "retry_count" INTEGER DEFAULT 0,
    "created_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "campaign_recipients_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "public"."campaigns" (
    "id" VARCHAR NOT NULL DEFAULT gen_random_uuid(),
    "channel_id" VARCHAR,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "campaign_type" TEXT NOT NULL,
    "type" TEXT NOT NULL,
    "api_type" TEXT NOT NULL,
    "template_id" VARCHAR,
    "template_name" TEXT,
    "template_language" TEXT,
    "variable_mapping" JSONB DEFAULT '{}',
    "contact_groups" JSONB DEFAULT '[]',
    "csv_data" JSONB DEFAULT '[]',
    "api_key" VARCHAR,
    "api_endpoint" TEXT,
    "status" TEXT DEFAULT 'draft',
    "scheduled_at" TIMESTAMP(6),
    "recipient_count" INTEGER DEFAULT 0,
    "sent_count" INTEGER DEFAULT 0,
    "delivered_count" INTEGER DEFAULT 0,
    "read_count" INTEGER DEFAULT 0,
    "replied_count" INTEGER DEFAULT 0,
    "failed_count" INTEGER DEFAULT 0,
    "completed_at" TIMESTAMP(6),
    "created_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "campaigns_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "public"."channels" (
    "id" VARCHAR NOT NULL DEFAULT gen_random_uuid(),
    "name" TEXT NOT NULL,
    "phone_number_id" TEXT NOT NULL,
    "access_token" TEXT NOT NULL,
    "whatsapp_business_account_id" TEXT,
    "phone_number" TEXT,
    "is_active" BOOLEAN DEFAULT true,
    "health_status" TEXT DEFAULT 'unknown',
    "last_health_check" TIMESTAMP(6),
    "health_details" JSONB DEFAULT '{}',
    "created_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "channels_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "public"."contacts" (
    "id" VARCHAR NOT NULL DEFAULT gen_random_uuid(),
    "channel_id" VARCHAR,
    "name" TEXT NOT NULL,
    "phone" TEXT NOT NULL,
    "email" TEXT,
    "groups" JSONB DEFAULT '[]',
    "tags" JSONB DEFAULT '[]',
    "status" TEXT DEFAULT 'active',
    "last_contact" TIMESTAMP(6),
    "created_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "contacts_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "public"."conversation_assignments" (
    "id" VARCHAR NOT NULL DEFAULT gen_random_uuid(),
    "conversation_id" VARCHAR NOT NULL,
    "user_id" VARCHAR NOT NULL,
    "assigned_by" VARCHAR,
    "assigned_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,
    "status" TEXT NOT NULL DEFAULT 'active',
    "priority" TEXT DEFAULT 'normal',
    "notes" TEXT,
    "resolved_at" TIMESTAMP(6),
    "created_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "conversation_assignments_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "public"."conversations" (
    "id" VARCHAR NOT NULL DEFAULT gen_random_uuid(),
    "channel_id" VARCHAR,
    "contact_id" VARCHAR,
    "assigned_to" VARCHAR,
    "contact_phone" VARCHAR,
    "contact_name" VARCHAR,
    "status" TEXT DEFAULT 'open',
    "priority" TEXT DEFAULT 'normal',
    "tags" JSONB DEFAULT '[]',
    "unread_count" INTEGER DEFAULT 0,
    "last_message_at" TIMESTAMP(6),
    "last_message_text" TEXT,
    "created_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "conversations_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "public"."message_queue" (
    "id" VARCHAR NOT NULL DEFAULT gen_random_uuid(),
    "campaign_id" VARCHAR,
    "channel_id" VARCHAR,
    "recipient_phone" VARCHAR(20) NOT NULL,
    "template_name" VARCHAR(100),
    "template_params" JSONB DEFAULT '[]',
    "message_type" VARCHAR(20) NOT NULL,
    "status" VARCHAR(20) DEFAULT 'queued',
    "attempts" INTEGER DEFAULT 0,
    "whatsapp_message_id" VARCHAR(100),
    "conversation_id" VARCHAR(100),
    "sent_via" VARCHAR(20),
    "cost" VARCHAR(20),
    "error_code" VARCHAR(50),
    "error_message" TEXT,
    "scheduled_for" TIMESTAMP(6),
    "processed_at" TIMESTAMP(6),
    "delivered_at" TIMESTAMP(6),
    "read_at" TIMESTAMP(6),
    "created_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "message_queue_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "public"."messages" (
    "id" VARCHAR NOT NULL DEFAULT gen_random_uuid(),
    "conversation_id" VARCHAR,
    "whatsapp_message_id" VARCHAR,
    "from_user" BOOLEAN DEFAULT false,
    "direction" VARCHAR DEFAULT 'outbound',
    "content" TEXT NOT NULL,
    "type" TEXT DEFAULT 'text',
    "message_type" VARCHAR,
    "status" TEXT DEFAULT 'sent',
    "timestamp" TIMESTAMP(6),
    "metadata" JSONB DEFAULT '{}',
    "delivered_at" TIMESTAMP(6),
    "read_at" TIMESTAMP(6),
    "error_code" VARCHAR(50),
    "error_message" TEXT,
    "error_details" JSONB,
    "campaign_id" VARCHAR,
    "created_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,
    "media_id" VARCHAR,
    "media_url" TEXT,
    "media_mime_type" VARCHAR(100),
    "media_sha256" VARCHAR(128),

    CONSTRAINT "messages_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "public"."panel_config" (
    "id" VARCHAR NOT NULL DEFAULT gen_random_uuid(),
    "name" VARCHAR NOT NULL,
    "tagline" VARCHAR,
    "description" TEXT,
    "logo" VARCHAR,
    "favicon" VARCHAR,
    "default_language" VARCHAR(5) DEFAULT 'en',
    "supported_languages" JSONB DEFAULT '["en"]',
    "company_name" VARCHAR,
    "company_website" VARCHAR,
    "support_email" VARCHAR,
    "created_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "panel_config_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "public"."session" (
    "sid" VARCHAR NOT NULL,
    "sess" JSON NOT NULL,
    "expire" TIMESTAMP(6) NOT NULL,

    CONSTRAINT "session_pkey" PRIMARY KEY ("sid")
);

-- CreateTable
CREATE TABLE "public"."templates" (
    "id" VARCHAR NOT NULL DEFAULT gen_random_uuid(),
    "channel_id" VARCHAR,
    "name" TEXT NOT NULL,
    "category" TEXT NOT NULL,
    "language" TEXT DEFAULT 'en_US',
    "header" TEXT,
    "body" TEXT NOT NULL,
    "footer" TEXT,
    "buttons" JSONB DEFAULT '[]',
    "variables" JSONB DEFAULT '[]',
    "status" TEXT DEFAULT 'draft',
    "rejection_reason" TEXT,
    "media_type" TEXT DEFAULT 'text',
    "media_url" TEXT,
    "media_handle" TEXT,
    "carousel_cards" JSONB DEFAULT '[]',
    "whatsapp_template_id" TEXT,
    "usage_count" INTEGER DEFAULT 0,
    "created_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "templates_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "public"."user_activity_logs" (
    "id" VARCHAR NOT NULL DEFAULT gen_random_uuid(),
    "user_id" VARCHAR NOT NULL,
    "action" TEXT NOT NULL,
    "entity_type" TEXT,
    "entity_id" VARCHAR,
    "details" JSONB DEFAULT '{}',
    "ip_address" TEXT,
    "user_agent" TEXT,
    "created_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "user_activity_logs_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "public"."users" (
    "id" VARCHAR NOT NULL DEFAULT gen_random_uuid(),
    "username" TEXT NOT NULL,
    "password" TEXT NOT NULL,
    "email" TEXT NOT NULL,
    "first_name" TEXT,
    "last_name" TEXT,
    "role" TEXT NOT NULL DEFAULT 'admin',
    "avatar" TEXT,
    "status" TEXT NOT NULL DEFAULT 'active',
    "permissions" TEXT[],
    "last_login" TIMESTAMP(6),
    "created_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "users_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "public"."webhook_configs" (
    "id" VARCHAR NOT NULL DEFAULT gen_random_uuid(),
    "channel_id" VARCHAR,
    "webhook_url" TEXT NOT NULL,
    "verify_token" VARCHAR(100) NOT NULL,
    "app_secret" TEXT,
    "events" JSONB NOT NULL DEFAULT '[]',
    "is_active" BOOLEAN DEFAULT true,
    "last_ping_at" TIMESTAMP(6),
    "created_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "webhook_configs_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "public"."whatsapp_channels" (
    "id" VARCHAR NOT NULL DEFAULT gen_random_uuid(),
    "name" TEXT NOT NULL,
    "phone_number" VARCHAR(20) NOT NULL,
    "phone_number_id" VARCHAR(50) NOT NULL,
    "waba_id" VARCHAR(50) NOT NULL,
    "access_token" TEXT NOT NULL,
    "business_account_id" VARCHAR(50),
    "rate_limit_tier" VARCHAR(20) DEFAULT 'standard',
    "quality_rating" VARCHAR(20) DEFAULT 'green',
    "status" VARCHAR(20) DEFAULT 'inactive',
    "error_message" TEXT,
    "last_health_check" TIMESTAMP(6),
    "message_limit" INTEGER,
    "messages_used" INTEGER,
    "created_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "whatsapp_channels_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "automation_edges_unique_idx" ON "public"."automation_edges"("automation_id", "source_node_id", "target_node_id");

-- CreateIndex
CREATE INDEX "automation_execution_logs_execution_idx" ON "public"."automation_execution_logs"("execution_id");

-- CreateIndex
CREATE INDEX "automation_executions_automation_idx" ON "public"."automation_executions"("automation_id");

-- CreateIndex
CREATE INDEX "automation_executions_status_idx" ON "public"."automation_executions"("status");

-- CreateIndex
CREATE INDEX "automation_nodes_automation_idx" ON "public"."automation_nodes"("automation_id");

-- CreateIndex
CREATE UNIQUE INDEX "automation_nodes_unique_idx" ON "public"."automation_nodes"("automation_id", "node_id");

-- CreateIndex
CREATE INDEX "automations_channel_idx" ON "public"."automations"("channel_id");

-- CreateIndex
CREATE INDEX "automations_status_idx" ON "public"."automations"("status");

-- CreateIndex
CREATE INDEX "recipients_campaign_idx" ON "public"."campaign_recipients"("campaign_id");

-- CreateIndex
CREATE INDEX "recipients_phone_idx" ON "public"."campaign_recipients"("phone");

-- CreateIndex
CREATE INDEX "recipients_status_idx" ON "public"."campaign_recipients"("status");

-- CreateIndex
CREATE UNIQUE INDEX "campaign_phone_unique" ON "public"."campaign_recipients"("campaign_id", "phone");

-- CreateIndex
CREATE INDEX "campaigns_channel_idx" ON "public"."campaigns"("channel_id");

-- CreateIndex
CREATE INDEX "campaigns_created_idx" ON "public"."campaigns"("created_at");

-- CreateIndex
CREATE INDEX "campaigns_status_idx" ON "public"."campaigns"("status");

-- CreateIndex
CREATE INDEX "contacts_channel_idx" ON "public"."contacts"("channel_id");

-- CreateIndex
CREATE INDEX "contacts_phone_idx" ON "public"."contacts"("phone");

-- CreateIndex
CREATE INDEX "contacts_status_idx" ON "public"."contacts"("status");

-- CreateIndex
CREATE UNIQUE INDEX "contacts_channel_phone_unique" ON "public"."contacts"("channel_id", "phone");

-- CreateIndex
CREATE INDEX "conversations_channel_idx" ON "public"."conversations"("channel_id");

-- CreateIndex
CREATE INDEX "conversations_contact_idx" ON "public"."conversations"("contact_id");

-- CreateIndex
CREATE INDEX "conversations_phone_idx" ON "public"."conversations"("contact_phone");

-- CreateIndex
CREATE INDEX "conversations_status_idx" ON "public"."conversations"("status");

-- CreateIndex
CREATE INDEX "messages_conversation_idx" ON "public"."messages"("conversation_id");

-- CreateIndex
CREATE INDEX "messages_created_idx" ON "public"."messages"("created_at");

-- CreateIndex
CREATE INDEX "messages_direction_idx" ON "public"."messages"("direction");

-- CreateIndex
CREATE INDEX "messages_status_idx" ON "public"."messages"("status");

-- CreateIndex
CREATE INDEX "messages_timestamp_idx" ON "public"."messages"("timestamp");

-- CreateIndex
CREATE INDEX "messages_whatsapp_idx" ON "public"."messages"("whatsapp_message_id");

-- CreateIndex
CREATE INDEX "IDX_session_expire" ON "public"."session"("expire");

-- CreateIndex
CREATE UNIQUE INDEX "users_username_unique" ON "public"."users"("username");

-- CreateIndex
CREATE UNIQUE INDEX "users_email_unique" ON "public"."users"("email");

-- CreateIndex
CREATE UNIQUE INDEX "whatsapp_channels_phone_number_unique" ON "public"."whatsapp_channels"("phone_number");

-- AddForeignKey
ALTER TABLE "public"."api_logs" ADD CONSTRAINT "api_logs_channel_id_channels_id_fk" FOREIGN KEY ("channel_id") REFERENCES "public"."channels"("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE "public"."automation_edges" ADD CONSTRAINT "automation_edges_automation_id_automations_id_fk" FOREIGN KEY ("automation_id") REFERENCES "public"."automations"("id") ON DELETE CASCADE ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE "public"."automation_executions" ADD CONSTRAINT "automation_executions_automation_id_automations_id_fk" FOREIGN KEY ("automation_id") REFERENCES "public"."automations"("id") ON DELETE CASCADE ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE "public"."automation_executions" ADD CONSTRAINT "automation_executions_contact_id_contacts_id_fk" FOREIGN KEY ("contact_id") REFERENCES "public"."contacts"("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE "public"."automation_executions" ADD CONSTRAINT "automation_executions_conversation_id_conversations_id_fk" FOREIGN KEY ("conversation_id") REFERENCES "public"."conversations"("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE "public"."automation_nodes" ADD CONSTRAINT "automation_nodes_automation_id_automations_id_fk" FOREIGN KEY ("automation_id") REFERENCES "public"."automations"("id") ON DELETE CASCADE ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE "public"."automations" ADD CONSTRAINT "automations_channel_id_channels_id_fk" FOREIGN KEY ("channel_id") REFERENCES "public"."channels"("id") ON DELETE CASCADE ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE "public"."automations" ADD CONSTRAINT "automations_created_by_users_id_fk" FOREIGN KEY ("created_by") REFERENCES "public"."users"("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE "public"."campaign_recipients" ADD CONSTRAINT "campaign_recipients_campaign_id_campaigns_id_fk" FOREIGN KEY ("campaign_id") REFERENCES "public"."campaigns"("id") ON DELETE CASCADE ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE "public"."campaign_recipients" ADD CONSTRAINT "campaign_recipients_contact_id_contacts_id_fk" FOREIGN KEY ("contact_id") REFERENCES "public"."contacts"("id") ON DELETE CASCADE ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE "public"."campaigns" ADD CONSTRAINT "campaigns_channel_id_channels_id_fk" FOREIGN KEY ("channel_id") REFERENCES "public"."channels"("id") ON DELETE CASCADE ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE "public"."campaigns" ADD CONSTRAINT "campaigns_template_id_templates_id_fk" FOREIGN KEY ("template_id") REFERENCES "public"."templates"("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE "public"."contacts" ADD CONSTRAINT "contacts_channel_id_channels_id_fk" FOREIGN KEY ("channel_id") REFERENCES "public"."channels"("id") ON DELETE CASCADE ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE "public"."conversation_assignments" ADD CONSTRAINT "conversation_assignments_assigned_by_users_id_fk" FOREIGN KEY ("assigned_by") REFERENCES "public"."users"("id") ON DELETE CASCADE ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE "public"."conversation_assignments" ADD CONSTRAINT "conversation_assignments_conversation_id_conversations_id_fk" FOREIGN KEY ("conversation_id") REFERENCES "public"."conversations"("id") ON DELETE CASCADE ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE "public"."conversation_assignments" ADD CONSTRAINT "conversation_assignments_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE CASCADE ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE "public"."conversations" ADD CONSTRAINT "conversations_channel_id_channels_id_fk" FOREIGN KEY ("channel_id") REFERENCES "public"."channels"("id") ON DELETE CASCADE ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE "public"."conversations" ADD CONSTRAINT "conversations_contact_id_contacts_id_fk" FOREIGN KEY ("contact_id") REFERENCES "public"."contacts"("id") ON DELETE CASCADE ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE "public"."message_queue" ADD CONSTRAINT "message_queue_campaign_id_campaigns_id_fk" FOREIGN KEY ("campaign_id") REFERENCES "public"."campaigns"("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE "public"."message_queue" ADD CONSTRAINT "message_queue_channel_id_whatsapp_channels_id_fk" FOREIGN KEY ("channel_id") REFERENCES "public"."whatsapp_channels"("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE "public"."messages" ADD CONSTRAINT "messages_campaign_id_campaigns_id_fk" FOREIGN KEY ("campaign_id") REFERENCES "public"."campaigns"("id") ON DELETE SET NULL ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE "public"."messages" ADD CONSTRAINT "messages_conversation_id_conversations_id_fk" FOREIGN KEY ("conversation_id") REFERENCES "public"."conversations"("id") ON DELETE CASCADE ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE "public"."templates" ADD CONSTRAINT "templates_channel_id_channels_id_fk" FOREIGN KEY ("channel_id") REFERENCES "public"."channels"("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE "public"."user_activity_logs" ADD CONSTRAINT "user_activity_logs_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE CASCADE ON UPDATE NO ACTION;

