-- Current sql file was generated after introspecting the database
-- If you want to run this migration please uncomment this code before executing migrations
/*
CREATE TYPE "public"."admin_role" AS ENUM('superadmin', 'support');--> statement-breakpoint
CREATE TYPE "public"."agent_role" AS ENUM('orders', 'reservations', 'campaigns', 'voice', 'chat', 'general');--> statement-breakpoint
CREATE TYPE "public"."blog_status" AS ENUM('draft', 'published', 'scheduled', 'archived');--> statement-breakpoint
CREATE TYPE "public"."booking_status" AS ENUM('pending', 'confirmed', 'seated', 'completed', 'cancelled', 'noshow', 'escalated', 'reminder_sent');--> statement-breakpoint
CREATE TYPE "public"."channel_type" AS ENUM('chat', 'voice', 'whatsapp', 'email', 'sms');--> statement-breakpoint
CREATE TYPE "public"."conversation_status" AS ENUM('ai', 'human', 'resolved');--> statement-breakpoint
CREATE TYPE "public"."kb_type" AS ENUM('doc', 'url', 'qa', 'article');--> statement-breakpoint
CREATE TYPE "public"."notification_type" AS ENUM('order', 'booking', 'ai', 'system', 'billing', 'staff');--> statement-breakpoint
CREATE TYPE "public"."order_status" AS ENUM('pending', 'confirmed', 'kitchen', 'ready', 'delivered', 'completed', 'cancelled', 'failed', 'escalated', 'processing', 'rider');--> statement-breakpoint
CREATE TYPE "public"."phone_type" AS ENUM('purchased', 'sip');--> statement-breakpoint
CREATE TYPE "public"."plan_status" AS ENUM('active', 'inactive', 'trial', 'cancelled', 'past_due');--> statement-breakpoint
CREATE TYPE "public"."ticket_priority" AS ENUM('low', 'medium', 'high', 'urgent');--> statement-breakpoint
CREATE TYPE "public"."ticket_status" AS ENUM('open', 'in_progress', 'resolved', 'closed');--> statement-breakpoint
CREATE TYPE "public"."user_role" AS ENUM('owner', 'manager', 'staff', 'superadmin', 'support');--> statement-breakpoint
CREATE TABLE "customers" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"restaurant_id" uuid NOT NULL,
	"name" text NOT NULL,
	"email" text,
	"phone" text,
	"preferences" jsonb DEFAULT '[]'::jsonb,
	"tags" jsonb DEFAULT '[]'::jsonb,
	"total_visits" integer DEFAULT 0,
	"total_spend" numeric(10, 2) DEFAULT '0',
	"tier" text DEFAULT 'bronze',
	"created_at" timestamp with time zone DEFAULT now(),
	"updated_at" timestamp with time zone DEFAULT now(),
	"notes" text
);
--> statement-breakpoint
CREATE TABLE "admin_users" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"email" text NOT NULL,
	"password_hash" text NOT NULL,
	"role" "admin_role" DEFAULT 'support',
	"is_active" boolean DEFAULT true,
	"created_at" timestamp with time zone DEFAULT now(),
	CONSTRAINT "admin_users_email_key" UNIQUE("email")
);
--> statement-breakpoint
CREATE TABLE "plans" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"name" text NOT NULL,
	"price_monthly" numeric(10, 2) DEFAULT '0' NOT NULL,
	"price_annual" numeric(10, 2) DEFAULT '0' NOT NULL,
	"features" jsonb DEFAULT '[]'::jsonb,
	"limits" jsonb DEFAULT '{}'::jsonb,
	"is_active" boolean DEFAULT true,
	"created_at" timestamp with time zone DEFAULT now(),
	"stripe_price_id_monthly" text,
	"stripe_price_id_annual" text,
	"stripe_product_id" text,
	"description" text,
	"trial_days" integer DEFAULT 14,
	"slug" text
);
--> statement-breakpoint
CREATE TABLE "subscriptions" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"restaurant_id" uuid NOT NULL,
	"plan_id" uuid,
	"status" "plan_status" DEFAULT 'trial',
	"current_period_end" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now(),
	"stripe_customer_id" text,
	"stripe_subscription_id" text,
	"stripe_price_id" text,
	"trial_start" timestamp with time zone,
	"trial_end" timestamp with time zone,
	CONSTRAINT "subscriptions_restaurant_id_key" UNIQUE("restaurant_id")
);
--> statement-breakpoint
CREATE TABLE "branches" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"restaurant_id" uuid NOT NULL,
	"name" text NOT NULL,
	"address" text,
	"phone" text,
	"hours" jsonb DEFAULT '{}'::jsonb,
	"timezone" text DEFAULT 'UTC',
	"is_active" boolean DEFAULT true,
	"created_at" timestamp with time zone DEFAULT now(),
	"updated_at" timestamp with time zone DEFAULT now(),
	"smtp_host" text,
	"smtp_port" integer,
	"smtp_user" text,
	"smtp_pass" text,
	"smtp_from" text
);
--> statement-breakpoint
CREATE TABLE "restaurants" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"name" text NOT NULL,
	"owner_id" uuid NOT NULL,
	"plan" text DEFAULT 'starter',
	"status" text DEFAULT 'active',
	"logo_url" text,
	"created_at" timestamp with time zone DEFAULT now(),
	"updated_at" timestamp with time zone DEFAULT now(),
	"currency" text,
	"currency_locked" boolean DEFAULT false,
	"currency_set_at" timestamp with time zone,
	"cuisine_type" text,
	"phone" text,
	"address" text,
	"description" text,
	"seating_capacity" integer,
	"display_id" text
);
--> statement-breakpoint
CREATE TABLE "users" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"restaurant_id" uuid,
	"branch_id" uuid,
	"role" "user_role" DEFAULT 'staff',
	"name" text DEFAULT '' NOT NULL,
	"email" text NOT NULL,
	"password_hash" text NOT NULL,
	"is_active" boolean DEFAULT true,
	"created_at" timestamp with time zone DEFAULT now(),
	"updated_at" timestamp with time zone DEFAULT now(),
	"preferred_language" text DEFAULT 'en',
	CONSTRAINT "users_email_key" UNIQUE("email")
);
--> statement-breakpoint
CREATE TABLE "orders" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"restaurant_id" uuid NOT NULL,
	"branch_id" uuid,
	"customer_id" uuid,
	"customer_name" text,
	"customer_phone" text,
	"items" jsonb DEFAULT '[]'::jsonb,
	"status" "order_status" DEFAULT 'pending',
	"channel" "channel_type" DEFAULT 'chat',
	"delivery_type" text DEFAULT 'dine-in',
	"table_number" text,
	"subtotal" numeric(10, 2) DEFAULT '0',
	"tax" numeric(10, 2) DEFAULT '0',
	"total" numeric(10, 2) DEFAULT '0',
	"ai_confidence" integer,
	"agent_id" text,
	"conversation_id" text,
	"special_instructions" text,
	"transcript_summary" text,
	"is_modified" boolean DEFAULT false,
	"modification_note" text,
	"created_at" timestamp with time zone DEFAULT now(),
	"updated_at" timestamp with time zone DEFAULT now(),
	"delivery_address" text,
	"order_number" integer NOT NULL,
	"customer_email" text
);
--> statement-breakpoint
CREATE TABLE "bookings" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"restaurant_id" uuid NOT NULL,
	"branch_id" uuid,
	"customer_id" uuid,
	"guest_name" text NOT NULL,
	"guest_phone" text,
	"guest_email" text,
	"table_no" text,
	"party_size" integer DEFAULT 1,
	"booking_date" date,
	"booking_time" text,
	"zone" text,
	"status" "booking_status" DEFAULT 'pending',
	"channel" "channel_type" DEFAULT 'chat',
	"occasion" text,
	"dietary_needs" jsonb DEFAULT '[]'::jsonb,
	"special_requests" text,
	"ai_notes" text,
	"ai_confidence" integer,
	"reminder_sent" boolean DEFAULT false,
	"is_vip" boolean DEFAULT false,
	"confirmation_channel" text DEFAULT 'none',
	"agent_id" text,
	"conversation_id" text,
	"notes" text,
	"created_at" timestamp with time zone DEFAULT now(),
	"updated_at" timestamp with time zone DEFAULT now()
);
--> statement-breakpoint
CREATE TABLE "menu_categories" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"restaurant_id" uuid NOT NULL,
	"branch_id" uuid,
	"name" text NOT NULL,
	"display_order" integer DEFAULT 0,
	"is_active" boolean DEFAULT true,
	"created_at" timestamp with time zone DEFAULT now()
);
--> statement-breakpoint
CREATE TABLE "phone_numbers" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"restaurant_id" uuid NOT NULL,
	"branch_id" uuid,
	"number" text NOT NULL,
	"type" "phone_type" DEFAULT 'purchased',
	"sip_config" jsonb DEFAULT '{}'::jsonb,
	"is_active" boolean DEFAULT true,
	"created_at" timestamp with time zone DEFAULT now(),
	"assigned_agent_id" uuid,
	"display_name" text,
	"updated_at" timestamp with time zone DEFAULT now(),
	"call_recording" boolean DEFAULT false,
	"call_transcription" boolean DEFAULT true,
	"greeting_override" text,
	"twilio_number_sid" text,
	"fallback_number" text
);
--> statement-breakpoint
CREATE TABLE "conversations" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"restaurant_id" uuid NOT NULL,
	"branch_id" uuid,
	"customer_id" uuid,
	"customer_name" text,
	"customer_avatar" text,
	"channel" "channel_type" DEFAULT 'chat',
	"status" "conversation_status" DEFAULT 'ai',
	"messages" jsonb DEFAULT '[]'::jsonb,
	"topic" text,
	"unread_count" integer DEFAULT 0,
	"ai_confidence" integer,
	"assigned_agent" text,
	"created_at" timestamp with time zone DEFAULT now(),
	"updated_at" timestamp with time zone DEFAULT now()
);
--> statement-breakpoint
CREATE TABLE "ai_agent_configs" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"restaurant_id" uuid NOT NULL,
	"branch_id" uuid,
	"voice_enabled" boolean DEFAULT true,
	"chat_enabled" boolean DEFAULT true,
	"model" text DEFAULT 'gpt-4o',
	"voice_model" text DEFAULT 'nova',
	"response_style" text DEFAULT 'friendly',
	"system_prompt" text,
	"greeting_script" text,
	"closing_script" text,
	"fallback_rules" jsonb DEFAULT '[]'::jsonb,
	"capabilities" jsonb DEFAULT '{}'::jsonb,
	"twilio_phone_number" text,
	"inbound_routing" text DEFAULT 'ai_agent',
	"outbound_enabled" boolean DEFAULT true,
	"call_greeting" text DEFAULT 'Hello! Thank you for calling. I am your AI assistant. How can I help you today?',
	"call_recording_enabled" boolean DEFAULT false,
	"call_transcription_enabled" boolean DEFAULT true,
	"created_at" timestamp with time zone DEFAULT now(),
	"updated_at" timestamp with time zone DEFAULT now()
);
--> statement-breakpoint
CREATE TABLE "knowledge_base" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"restaurant_id" uuid NOT NULL,
	"branch_id" uuid,
	"type" "kb_type" DEFAULT 'doc',
	"title" text NOT NULL,
	"content" text,
	"url" text,
	"question" text,
	"answer" text,
	"file_size" text,
	"file_type" text,
	"pages" integer,
	"status" text DEFAULT 'active',
	"is_active" boolean DEFAULT true,
	"created_at" timestamp with time zone DEFAULT now(),
	"updated_at" timestamp with time zone DEFAULT now(),
	"embedding" vector(1536),
	"embedding_status" text DEFAULT 'pending' NOT NULL,
	"embedding_error" text
);
--> statement-breakpoint
CREATE TABLE "staff" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"restaurant_id" uuid NOT NULL,
	"branch_id" uuid,
	"user_id" uuid,
	"name" text NOT NULL,
	"email" text,
	"role" text DEFAULT 'staff',
	"permissions" jsonb DEFAULT '[]'::jsonb,
	"is_active" boolean DEFAULT true,
	"last_active_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now(),
	"role_id" uuid
);
--> statement-breakpoint
CREATE TABLE "webhooks" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"restaurant_id" uuid NOT NULL,
	"endpoint_url" text NOT NULL,
	"events" jsonb DEFAULT '[]'::jsonb,
	"secret" text,
	"status" text DEFAULT 'active',
	"success_rate" numeric(5, 2) DEFAULT '100',
	"total_deliveries" integer DEFAULT 0,
	"created_at" timestamp with time zone DEFAULT now(),
	"updated_at" timestamp with time zone DEFAULT now()
);
--> statement-breakpoint
CREATE TABLE "api_keys" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"restaurant_id" uuid NOT NULL,
	"name" text NOT NULL,
	"key_hash" text NOT NULL,
	"key_preview" text,
	"permissions" jsonb DEFAULT '[]'::jsonb,
	"calls_this_month" integer DEFAULT 0,
	"status" text DEFAULT 'active',
	"last_used_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now()
);
--> statement-breakpoint
CREATE TABLE "notifications" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"restaurant_id" uuid NOT NULL,
	"user_id" uuid,
	"type" "notification_type" DEFAULT 'system',
	"title" text NOT NULL,
	"message" text,
	"is_read" boolean DEFAULT false,
	"action_label" text,
	"action_href" text,
	"created_at" timestamp with time zone DEFAULT now()
);
--> statement-breakpoint
CREATE TABLE "support_tickets" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"restaurant_id" uuid NOT NULL,
	"user_id" uuid,
	"subject" text NOT NULL,
	"category" text DEFAULT 'Other',
	"priority" "ticket_priority" DEFAULT 'medium',
	"status" "ticket_status" DEFAULT 'open',
	"description" text,
	"messages" jsonb DEFAULT '[]'::jsonb,
	"created_at" timestamp with time zone DEFAULT now(),
	"updated_at" timestamp with time zone DEFAULT now()
);
--> statement-breakpoint
CREATE TABLE "blog_posts" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"author_id" uuid,
	"title" text NOT NULL,
	"slug" text NOT NULL,
	"content" jsonb DEFAULT '[]'::jsonb,
	"hero_image" text,
	"excerpt" text,
	"category" text DEFAULT 'General',
	"tags" jsonb DEFAULT '[]'::jsonb,
	"status" "blog_status" DEFAULT 'draft',
	"views" integer DEFAULT 0,
	"published_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now(),
	"updated_at" timestamp with time zone DEFAULT now(),
	CONSTRAINT "blog_posts_slug_key" UNIQUE("slug")
);
--> statement-breakpoint
CREATE TABLE "call_logs" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"restaurant_id" uuid NOT NULL,
	"branch_id" uuid,
	"conversation_id" uuid,
	"call_sid" text,
	"direction" text DEFAULT 'inbound' NOT NULL,
	"from_number" text,
	"to_number" text,
	"status" text DEFAULT 'initiated',
	"duration_seconds" integer DEFAULT 0,
	"transcript" text,
	"recording_url" text,
	"ai_handled" boolean DEFAULT true,
	"escalated_to_human" boolean DEFAULT false,
	"escalation_reason" text,
	"started_at" timestamp with time zone DEFAULT now(),
	"ended_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now(),
	"updated_at" timestamp with time zone DEFAULT now()
);
--> statement-breakpoint
CREATE TABLE "password_reset_tokens" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"user_id" uuid NOT NULL,
	"token" text NOT NULL,
	"expires_at" timestamp with time zone NOT NULL,
	"used" boolean DEFAULT false,
	"created_at" timestamp with time zone DEFAULT now(),
	CONSTRAINT "password_reset_tokens_user_id_key" UNIQUE("user_id")
);
--> statement-breakpoint
CREATE TABLE "platform_settings" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"key" text NOT NULL,
	"value" jsonb DEFAULT '{}'::jsonb NOT NULL,
	"updated_by" uuid,
	"created_at" timestamp with time zone DEFAULT now(),
	"updated_at" timestamp with time zone DEFAULT now(),
	CONSTRAINT "platform_settings_key_key" UNIQUE("key")
);
--> statement-breakpoint
CREATE TABLE "notification_preferences" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"restaurant_id" uuid NOT NULL,
	"user_id" uuid,
	"preferences" jsonb DEFAULT '[]'::jsonb,
	"created_at" timestamp with time zone DEFAULT now(),
	"updated_at" timestamp with time zone DEFAULT now()
);
--> statement-breakpoint
CREATE TABLE "message_templates" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"restaurant_id" uuid NOT NULL,
	"template_key" text NOT NULL,
	"channel" text NOT NULL,
	"content" text NOT NULL,
	"created_at" timestamp with time zone DEFAULT now(),
	"updated_at" timestamp with time zone DEFAULT now(),
	CONSTRAINT "message_templates_restaurant_id_template_key_channel_key" UNIQUE("restaurant_id","template_key","channel")
);
--> statement-breakpoint
CREATE TABLE "audit_logs" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"actor_id" uuid,
	"actor_email" text,
	"actor_type" text DEFAULT 'system' NOT NULL,
	"action" text NOT NULL,
	"resource" text,
	"resource_id" uuid,
	"ip_address" text,
	"severity" text DEFAULT 'info' NOT NULL,
	"metadata" jsonb DEFAULT '{}'::jsonb,
	"created_at" timestamp with time zone DEFAULT now()
);
--> statement-breakpoint
CREATE TABLE "webhook_delivery_logs" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"webhook_id" uuid NOT NULL,
	"restaurant_id" uuid NOT NULL,
	"event" text NOT NULL,
	"status" text DEFAULT 'success' NOT NULL,
	"response_code" integer,
	"duration_ms" integer,
	"error_message" text,
	"triggered_at" timestamp with time zone DEFAULT now(),
	"response_body" text
);
--> statement-breakpoint
CREATE TABLE "menu_items" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"restaurant_id" uuid NOT NULL,
	"branch_id" uuid,
	"category_id" uuid,
	"name" text NOT NULL,
	"description" text,
	"price" numeric(10, 2) DEFAULT '0',
	"modifiers" jsonb DEFAULT '[]'::jsonb,
	"dietary_tags" jsonb DEFAULT '[]'::jsonb,
	"image_url" text,
	"is_available" boolean DEFAULT true,
	"created_at" timestamp with time zone DEFAULT now(),
	"updated_at" timestamp with time zone DEFAULT now(),
	"sort_order" integer DEFAULT 0,
	"spice_level" integer DEFAULT 0,
	"is_veg" boolean DEFAULT false,
	"prep_time_minutes" integer DEFAULT 0,
	"calories" integer DEFAULT 0
);
--> statement-breakpoint
CREATE TABLE "menu_modifier_groups" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"restaurant_id" uuid NOT NULL,
	"branch_id" uuid,
	"menu_item_id" uuid NOT NULL,
	"name" text NOT NULL,
	"type" text DEFAULT 'checkbox' NOT NULL,
	"is_required" boolean DEFAULT false,
	"min_selections" integer DEFAULT 0,
	"max_selections" integer,
	"display_order" integer DEFAULT 0,
	"created_at" timestamp with time zone DEFAULT now()
);
--> statement-breakpoint
CREATE TABLE "menu_modifier_options" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"modifier_group_id" uuid NOT NULL,
	"name" text NOT NULL,
	"price_delta" numeric(10, 2) DEFAULT '0',
	"is_default" boolean DEFAULT false,
	"is_available" boolean DEFAULT true,
	"display_order" integer DEFAULT 0,
	"created_at" timestamp with time zone DEFAULT now()
);
--> statement-breakpoint
CREATE TABLE "llm_providers" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"name" text NOT NULL,
	"display_name" text NOT NULL,
	"api_base_url" text NOT NULL,
	"auth_header_format" text DEFAULT 'Bearer' NOT NULL,
	"api_key_env_var" text,
	"is_active" boolean DEFAULT true,
	"display_order" integer DEFAULT 0,
	"created_at" timestamp with time zone DEFAULT now(),
	"updated_at" timestamp with time zone DEFAULT now(),
	CONSTRAINT "llm_providers_name_key" UNIQUE("name")
);
--> statement-breakpoint
CREATE TABLE "llm_models" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"provider_id" uuid NOT NULL,
	"model_id" text NOT NULL,
	"display_name" text NOT NULL,
	"context_length" integer DEFAULT 4096,
	"supports_functions" boolean DEFAULT false,
	"is_active" boolean DEFAULT true,
	"display_order" integer DEFAULT 0,
	"created_at" timestamp with time zone DEFAULT now(),
	"updated_at" timestamp with time zone DEFAULT now(),
	CONSTRAINT "llm_models_provider_id_model_id_key" UNIQUE("provider_id","model_id")
);
--> statement-breakpoint
CREATE TABLE "voice_providers" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"name" text NOT NULL,
	"display_name" text NOT NULL,
	"api_base_url" text NOT NULL,
	"api_key_env_var" text,
	"is_active" boolean DEFAULT true,
	"display_order" integer DEFAULT 0,
	"created_at" timestamp with time zone DEFAULT now(),
	"updated_at" timestamp with time zone DEFAULT now(),
	CONSTRAINT "voice_providers_name_key" UNIQUE("name")
);
--> statement-breakpoint
CREATE TABLE "voice_models" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"voice_provider_id" uuid NOT NULL,
	"voice_id" text NOT NULL,
	"display_name" text NOT NULL,
	"language_code" text DEFAULT 'en-US',
	"gender" text DEFAULT 'neutral',
	"is_active" boolean DEFAULT true,
	"display_order" integer DEFAULT 0,
	"created_at" timestamp with time zone DEFAULT now(),
	"updated_at" timestamp with time zone DEFAULT now(),
	CONSTRAINT "voice_models_voice_provider_id_voice_id_key" UNIQUE("voice_provider_id","voice_id")
);
--> statement-breakpoint
CREATE TABLE "restaurant_api_keys" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"restaurant_id" uuid NOT NULL,
	"provider_name" text NOT NULL,
	"encrypted_api_key" text NOT NULL,
	"key_hint" text NOT NULL,
	"is_active" boolean DEFAULT true,
	"created_at" timestamp with time zone DEFAULT now(),
	"updated_at" timestamp with time zone DEFAULT now(),
	CONSTRAINT "restaurant_api_keys_restaurant_id_provider_name_key" UNIQUE("restaurant_id","provider_name")
);
--> statement-breakpoint
CREATE TABLE "telephone_settings" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"restaurant_id" uuid NOT NULL,
	"twilio_connected" boolean DEFAULT false,
	"twilio_account_sid_hint" text,
	"created_at" timestamp with time zone DEFAULT now(),
	"updated_at" timestamp with time zone DEFAULT now(),
	"twilio_account_sid" text,
	"twilio_auth_token" text,
	CONSTRAINT "telephone_settings_restaurant_id_key" UNIQUE("restaurant_id")
);
--> statement-breakpoint
CREATE TABLE "sip_registrations" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"restaurant_id" uuid,
	"contact_uri" text NOT NULL,
	"aor" text NOT NULL,
	"expires_at" timestamp with time zone NOT NULL,
	"user_agent" text,
	"created_at" timestamp with time zone DEFAULT now(),
	"updated_at" timestamp with time zone DEFAULT now()
);
--> statement-breakpoint
CREATE TABLE "sip_call_sessions" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"restaurant_id" uuid NOT NULL,
	"phone_number_id" uuid,
	"agent_id" uuid,
	"sip_call_id" text,
	"caller_number" text,
	"callee_number" text,
	"direction" text DEFAULT 'inbound' NOT NULL,
	"status" text DEFAULT 'ringing' NOT NULL,
	"started_at" timestamp with time zone DEFAULT now(),
	"ended_at" timestamp with time zone,
	"duration_seconds" integer DEFAULT 0,
	"transcript" jsonb DEFAULT '[]'::jsonb,
	"escalated_to_human" boolean DEFAULT false,
	"metadata" jsonb DEFAULT '{}'::jsonb,
	"created_at" timestamp with time zone DEFAULT now(),
	"recording_url" text
);
--> statement-breakpoint
CREATE TABLE "widget_settings" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"restaurant_id" uuid NOT NULL,
	"primary_color" text DEFAULT '#f97316',
	"welcome_message" text DEFAULT 'Hi! How can we help you today?',
	"placeholder_text" text DEFAULT 'Type your message...',
	"position" text DEFAULT 'bottom-right',
	"auto_open_delay" integer DEFAULT 0,
	"bot_name" text DEFAULT 'AI Assistant',
	"show_branding" boolean DEFAULT true,
	"is_enabled" boolean DEFAULT true,
	"created_at" timestamp with time zone DEFAULT now(),
	"updated_at" timestamp with time zone DEFAULT now(),
	"agent_id" uuid,
	"icon_url" text,
	CONSTRAINT "widget_settings_restaurant_id_key" UNIQUE("restaurant_id")
);
--> statement-breakpoint
CREATE TABLE "ai_agents" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"restaurant_id" uuid NOT NULL,
	"branch_id" uuid,
	"name" text NOT NULL,
	"description" text,
	"role" "agent_role" DEFAULT 'general',
	"llm_model_id" uuid,
	"voice_model_id" uuid,
	"system_prompt" text,
	"greeting_script" text,
	"closing_script" text,
	"fallback_rules" jsonb DEFAULT '[]'::jsonb,
	"capabilities" jsonb DEFAULT '{}'::jsonb,
	"is_active" boolean DEFAULT true,
	"is_default" boolean DEFAULT false,
	"created_at" timestamp with time zone DEFAULT now(),
	"updated_at" timestamp with time zone DEFAULT now(),
	"voice_language_code" text DEFAULT 'hi-IN',
	"channels" text[] DEFAULT '{"chat","voice"}' NOT NULL,
	"realtime_model" text DEFAULT 'gpt-realtime-1.5',
	"vad_threshold" double precision DEFAULT 0.5,
	"vad_prefix_padding_ms" integer DEFAULT 300,
	"vad_silence_duration_ms" integer DEFAULT 600
);
--> statement-breakpoint
CREATE TABLE "roles" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"restaurant_id" uuid,
	"name" text NOT NULL,
	"description" text,
	"permissions" jsonb DEFAULT '{}'::jsonb NOT NULL,
	"is_system" boolean DEFAULT false NOT NULL,
	"created_at" timestamp with time zone DEFAULT now(),
	"updated_at" timestamp with time zone DEFAULT now()
);
--> statement-breakpoint
CREATE TABLE "alert_settings" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"restaurant_id" uuid NOT NULL,
	"sound_enabled" boolean DEFAULT true NOT NULL,
	"volume" integer DEFAULT 70 NOT NULL,
	"auto_dismiss" integer DEFAULT 30 NOT NULL,
	"show_orders" boolean DEFAULT true NOT NULL,
	"show_bookings" boolean DEFAULT true NOT NULL,
	"show_ai_escalations" boolean DEFAULT true NOT NULL,
	"browser_notifications" boolean DEFAULT false NOT NULL,
	"created_at" timestamp with time zone DEFAULT now(),
	"updated_at" timestamp with time zone DEFAULT now(),
	CONSTRAINT "alert_settings_restaurant_unique" UNIQUE("restaurant_id")
);
--> statement-breakpoint
CREATE TABLE "languages" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"code" text NOT NULL,
	"name" text NOT NULL,
	"native_name" text NOT NULL,
	"flag_emoji" text DEFAULT '' NOT NULL,
	"is_rtl" boolean DEFAULT false NOT NULL,
	"is_active" boolean DEFAULT true NOT NULL,
	"is_default" boolean DEFAULT false NOT NULL,
	"display_order" integer DEFAULT 0 NOT NULL,
	"created_at" timestamp with time zone DEFAULT now(),
	"updated_at" timestamp with time zone DEFAULT now(),
	CONSTRAINT "languages_code_key" UNIQUE("code")
);
--> statement-breakpoint
CREATE TABLE "translations" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"lang_code" text NOT NULL,
	"key_id" uuid NOT NULL,
	"value" text DEFAULT '' NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now(),
	CONSTRAINT "translations_lang_code_key_id_key" UNIQUE("lang_code","key_id")
);
--> statement-breakpoint
CREATE TABLE "translation_keys" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"key" text NOT NULL,
	"category" text DEFAULT 'common' NOT NULL,
	"description" text,
	"default_value" text DEFAULT '' NOT NULL,
	"created_at" timestamp with time zone DEFAULT now(),
	CONSTRAINT "translation_keys_key_key" UNIQUE("key")
);
--> statement-breakpoint
CREATE TABLE "agent_knowledge_bases" (
	"agent_id" uuid NOT NULL,
	"kb_id" uuid NOT NULL,
	CONSTRAINT "agent_knowledge_bases_pkey" PRIMARY KEY("agent_id","kb_id")
);
--> statement-breakpoint
ALTER TABLE "customers" ADD CONSTRAINT "customers_restaurant_id_fkey" FOREIGN KEY ("restaurant_id") REFERENCES "public"."restaurants"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "subscriptions" ADD CONSTRAINT "subscriptions_plan_id_fkey" FOREIGN KEY ("plan_id") REFERENCES "public"."plans"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "subscriptions" ADD CONSTRAINT "subscriptions_restaurant_id_fkey" FOREIGN KEY ("restaurant_id") REFERENCES "public"."restaurants"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "branches" ADD CONSTRAINT "branches_restaurant_id_fkey" FOREIGN KEY ("restaurant_id") REFERENCES "public"."restaurants"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "users" ADD CONSTRAINT "users_branch_id_fkey" FOREIGN KEY ("branch_id") REFERENCES "public"."branches"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "users" ADD CONSTRAINT "users_restaurant_id_fkey" FOREIGN KEY ("restaurant_id") REFERENCES "public"."restaurants"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "orders" ADD CONSTRAINT "orders_branch_id_fkey" FOREIGN KEY ("branch_id") REFERENCES "public"."branches"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "orders" ADD CONSTRAINT "orders_customer_id_fkey" FOREIGN KEY ("customer_id") REFERENCES "public"."customers"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "orders" ADD CONSTRAINT "orders_restaurant_id_fkey" FOREIGN KEY ("restaurant_id") REFERENCES "public"."restaurants"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "bookings" ADD CONSTRAINT "bookings_branch_id_fkey" FOREIGN KEY ("branch_id") REFERENCES "public"."branches"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "bookings" ADD CONSTRAINT "bookings_customer_id_fkey" FOREIGN KEY ("customer_id") REFERENCES "public"."customers"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "bookings" ADD CONSTRAINT "bookings_restaurant_id_fkey" FOREIGN KEY ("restaurant_id") REFERENCES "public"."restaurants"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "menu_categories" ADD CONSTRAINT "menu_categories_branch_id_fkey" FOREIGN KEY ("branch_id") REFERENCES "public"."branches"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "menu_categories" ADD CONSTRAINT "menu_categories_restaurant_id_fkey" FOREIGN KEY ("restaurant_id") REFERENCES "public"."restaurants"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "phone_numbers" ADD CONSTRAINT "phone_numbers_assigned_agent_id_fkey" FOREIGN KEY ("assigned_agent_id") REFERENCES "public"."ai_agents"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "phone_numbers" ADD CONSTRAINT "phone_numbers_branch_id_fkey" FOREIGN KEY ("branch_id") REFERENCES "public"."branches"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "phone_numbers" ADD CONSTRAINT "phone_numbers_restaurant_id_fkey" FOREIGN KEY ("restaurant_id") REFERENCES "public"."restaurants"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "conversations" ADD CONSTRAINT "conversations_branch_id_fkey" FOREIGN KEY ("branch_id") REFERENCES "public"."branches"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "conversations" ADD CONSTRAINT "conversations_customer_id_fkey" FOREIGN KEY ("customer_id") REFERENCES "public"."customers"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "conversations" ADD CONSTRAINT "conversations_restaurant_id_fkey" FOREIGN KEY ("restaurant_id") REFERENCES "public"."restaurants"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "ai_agent_configs" ADD CONSTRAINT "ai_agent_configs_branch_id_fkey" FOREIGN KEY ("branch_id") REFERENCES "public"."branches"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "ai_agent_configs" ADD CONSTRAINT "ai_agent_configs_restaurant_id_fkey" FOREIGN KEY ("restaurant_id") REFERENCES "public"."restaurants"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "knowledge_base" ADD CONSTRAINT "knowledge_base_branch_id_fkey" FOREIGN KEY ("branch_id") REFERENCES "public"."branches"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "knowledge_base" ADD CONSTRAINT "knowledge_base_restaurant_id_fkey" FOREIGN KEY ("restaurant_id") REFERENCES "public"."restaurants"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "staff" ADD CONSTRAINT "staff_branch_id_fkey" FOREIGN KEY ("branch_id") REFERENCES "public"."branches"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "staff" ADD CONSTRAINT "staff_restaurant_id_fkey" FOREIGN KEY ("restaurant_id") REFERENCES "public"."restaurants"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "staff" ADD CONSTRAINT "staff_role_id_fkey" FOREIGN KEY ("role_id") REFERENCES "public"."roles"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "staff" ADD CONSTRAINT "staff_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "webhooks" ADD CONSTRAINT "webhooks_restaurant_id_fkey" FOREIGN KEY ("restaurant_id") REFERENCES "public"."restaurants"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "api_keys" ADD CONSTRAINT "api_keys_restaurant_id_fkey" FOREIGN KEY ("restaurant_id") REFERENCES "public"."restaurants"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "notifications" ADD CONSTRAINT "notifications_restaurant_id_fkey" FOREIGN KEY ("restaurant_id") REFERENCES "public"."restaurants"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "notifications" ADD CONSTRAINT "notifications_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "support_tickets" ADD CONSTRAINT "support_tickets_restaurant_id_fkey" FOREIGN KEY ("restaurant_id") REFERENCES "public"."restaurants"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "support_tickets" ADD CONSTRAINT "support_tickets_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "call_logs" ADD CONSTRAINT "call_logs_branch_id_fkey" FOREIGN KEY ("branch_id") REFERENCES "public"."branches"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "call_logs" ADD CONSTRAINT "call_logs_conversation_id_fkey" FOREIGN KEY ("conversation_id") REFERENCES "public"."conversations"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "call_logs" ADD CONSTRAINT "call_logs_restaurant_id_fkey" FOREIGN KEY ("restaurant_id") REFERENCES "public"."restaurants"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "password_reset_tokens" ADD CONSTRAINT "password_reset_tokens_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "notification_preferences" ADD CONSTRAINT "notification_preferences_restaurant_id_fkey" FOREIGN KEY ("restaurant_id") REFERENCES "public"."restaurants"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "notification_preferences" ADD CONSTRAINT "notification_preferences_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "message_templates" ADD CONSTRAINT "message_templates_restaurant_id_fkey" FOREIGN KEY ("restaurant_id") REFERENCES "public"."restaurants"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "webhook_delivery_logs" ADD CONSTRAINT "webhook_delivery_logs_restaurant_id_fkey" FOREIGN KEY ("restaurant_id") REFERENCES "public"."restaurants"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "webhook_delivery_logs" ADD CONSTRAINT "webhook_delivery_logs_webhook_id_fkey" FOREIGN KEY ("webhook_id") REFERENCES "public"."webhooks"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "menu_items" ADD CONSTRAINT "menu_items_branch_id_fkey" FOREIGN KEY ("branch_id") REFERENCES "public"."branches"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "menu_items" ADD CONSTRAINT "menu_items_category_id_fkey" FOREIGN KEY ("category_id") REFERENCES "public"."menu_categories"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "menu_items" ADD CONSTRAINT "menu_items_restaurant_id_fkey" FOREIGN KEY ("restaurant_id") REFERENCES "public"."restaurants"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "menu_modifier_groups" ADD CONSTRAINT "menu_modifier_groups_branch_id_fkey" FOREIGN KEY ("branch_id") REFERENCES "public"."branches"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "menu_modifier_groups" ADD CONSTRAINT "menu_modifier_groups_menu_item_id_fkey" FOREIGN KEY ("menu_item_id") REFERENCES "public"."menu_items"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "menu_modifier_groups" ADD CONSTRAINT "menu_modifier_groups_restaurant_id_fkey" FOREIGN KEY ("restaurant_id") REFERENCES "public"."restaurants"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "menu_modifier_options" ADD CONSTRAINT "menu_modifier_options_modifier_group_id_fkey" FOREIGN KEY ("modifier_group_id") REFERENCES "public"."menu_modifier_groups"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "llm_models" ADD CONSTRAINT "llm_models_provider_id_fkey" FOREIGN KEY ("provider_id") REFERENCES "public"."llm_providers"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "voice_models" ADD CONSTRAINT "voice_models_voice_provider_id_fkey" FOREIGN KEY ("voice_provider_id") REFERENCES "public"."voice_providers"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "restaurant_api_keys" ADD CONSTRAINT "restaurant_api_keys_restaurant_id_fkey" FOREIGN KEY ("restaurant_id") REFERENCES "public"."restaurants"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "telephone_settings" ADD CONSTRAINT "telephone_settings_restaurant_id_fkey" FOREIGN KEY ("restaurant_id") REFERENCES "public"."restaurants"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "sip_registrations" ADD CONSTRAINT "sip_registrations_restaurant_id_fkey" FOREIGN KEY ("restaurant_id") REFERENCES "public"."restaurants"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "sip_call_sessions" ADD CONSTRAINT "sip_call_sessions_agent_id_fkey" FOREIGN KEY ("agent_id") REFERENCES "public"."ai_agents"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "sip_call_sessions" ADD CONSTRAINT "sip_call_sessions_phone_number_id_fkey" FOREIGN KEY ("phone_number_id") REFERENCES "public"."phone_numbers"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "sip_call_sessions" ADD CONSTRAINT "sip_call_sessions_restaurant_id_fkey" FOREIGN KEY ("restaurant_id") REFERENCES "public"."restaurants"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "widget_settings" ADD CONSTRAINT "widget_settings_agent_id_fkey" FOREIGN KEY ("agent_id") REFERENCES "public"."ai_agents"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "widget_settings" ADD CONSTRAINT "widget_settings_restaurant_id_fkey" FOREIGN KEY ("restaurant_id") REFERENCES "public"."restaurants"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "ai_agents" ADD CONSTRAINT "ai_agents_branch_id_fkey" FOREIGN KEY ("branch_id") REFERENCES "public"."branches"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "ai_agents" ADD CONSTRAINT "ai_agents_llm_model_id_fkey" FOREIGN KEY ("llm_model_id") REFERENCES "public"."llm_models"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "ai_agents" ADD CONSTRAINT "ai_agents_restaurant_id_fkey" FOREIGN KEY ("restaurant_id") REFERENCES "public"."restaurants"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "ai_agents" ADD CONSTRAINT "ai_agents_voice_model_id_fkey" FOREIGN KEY ("voice_model_id") REFERENCES "public"."voice_models"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "roles" ADD CONSTRAINT "roles_restaurant_id_fkey" FOREIGN KEY ("restaurant_id") REFERENCES "public"."restaurants"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "alert_settings" ADD CONSTRAINT "alert_settings_restaurant_id_fkey" FOREIGN KEY ("restaurant_id") REFERENCES "public"."restaurants"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "translations" ADD CONSTRAINT "translations_key_id_fkey" FOREIGN KEY ("key_id") REFERENCES "public"."translation_keys"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "translations" ADD CONSTRAINT "translations_lang_code_fkey" FOREIGN KEY ("lang_code") REFERENCES "public"."languages"("code") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "agent_knowledge_bases" ADD CONSTRAINT "agent_knowledge_bases_agent_id_fkey" FOREIGN KEY ("agent_id") REFERENCES "public"."ai_agents"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "agent_knowledge_bases" ADD CONSTRAINT "agent_knowledge_bases_kb_id_fkey" FOREIGN KEY ("kb_id") REFERENCES "public"."knowledge_base"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE INDEX "idx_customers_restaurant_id" ON "customers" USING btree ("restaurant_id" uuid_ops);--> statement-breakpoint
CREATE UNIQUE INDEX "idx_plans_slug" ON "plans" USING btree ("slug" text_ops);--> statement-breakpoint
CREATE INDEX "idx_branches_restaurant_id" ON "branches" USING btree ("restaurant_id" uuid_ops);--> statement-breakpoint
CREATE UNIQUE INDEX "idx_restaurants_display_id" ON "restaurants" USING btree ("display_id" text_ops);--> statement-breakpoint
CREATE INDEX "idx_restaurants_owner_id" ON "restaurants" USING btree ("owner_id" uuid_ops);--> statement-breakpoint
CREATE INDEX "idx_users_email" ON "users" USING btree ("email" text_ops);--> statement-breakpoint
CREATE INDEX "idx_users_restaurant_id" ON "users" USING btree ("restaurant_id" uuid_ops);--> statement-breakpoint
CREATE INDEX "idx_orders_branch_id" ON "orders" USING btree ("branch_id" uuid_ops);--> statement-breakpoint
CREATE INDEX "idx_orders_created_at" ON "orders" USING btree ("created_at" timestamptz_ops);--> statement-breakpoint
CREATE INDEX "idx_orders_restaurant_id" ON "orders" USING btree ("restaurant_id" uuid_ops);--> statement-breakpoint
CREATE UNIQUE INDEX "idx_orders_restaurant_order_number" ON "orders" USING btree ("restaurant_id" int4_ops,"order_number" uuid_ops);--> statement-breakpoint
CREATE INDEX "idx_orders_status" ON "orders" USING btree ("status" enum_ops);--> statement-breakpoint
CREATE INDEX "idx_bookings_branch_id" ON "bookings" USING btree ("branch_id" uuid_ops);--> statement-breakpoint
CREATE INDEX "idx_bookings_restaurant_id" ON "bookings" USING btree ("restaurant_id" uuid_ops);--> statement-breakpoint
CREATE INDEX "idx_bookings_status" ON "bookings" USING btree ("status" enum_ops);--> statement-breakpoint
CREATE INDEX "idx_menu_categories_restaurant_id" ON "menu_categories" USING btree ("restaurant_id" uuid_ops);--> statement-breakpoint
CREATE INDEX "idx_phone_numbers_assigned_agent" ON "phone_numbers" USING btree ("assigned_agent_id" uuid_ops);--> statement-breakpoint
CREATE INDEX "idx_phone_numbers_restaurant_id" ON "phone_numbers" USING btree ("restaurant_id" uuid_ops);--> statement-breakpoint
CREATE INDEX "idx_conversations_restaurant_id" ON "conversations" USING btree ("restaurant_id" uuid_ops);--> statement-breakpoint
CREATE INDEX "idx_conversations_status" ON "conversations" USING btree ("status" enum_ops);--> statement-breakpoint
CREATE INDEX "kb_embedding_idx" ON "knowledge_base" USING ivfflat ("embedding" vector_cosine_ops) WITH (lists=100);--> statement-breakpoint
CREATE INDEX "idx_notifications_is_read" ON "notifications" USING btree ("is_read" bool_ops);--> statement-breakpoint
CREATE INDEX "idx_notifications_restaurant_id" ON "notifications" USING btree ("restaurant_id" uuid_ops);--> statement-breakpoint
CREATE INDEX "idx_notifications_user_id" ON "notifications" USING btree ("user_id" uuid_ops);--> statement-breakpoint
CREATE INDEX "idx_support_tickets_restaurant_id" ON "support_tickets" USING btree ("restaurant_id" uuid_ops);--> statement-breakpoint
CREATE INDEX "idx_blog_posts_slug" ON "blog_posts" USING btree ("slug" text_ops);--> statement-breakpoint
CREATE INDEX "idx_blog_posts_status" ON "blog_posts" USING btree ("status" enum_ops);--> statement-breakpoint
CREATE INDEX "idx_call_logs_call_sid" ON "call_logs" USING btree ("call_sid" text_ops);--> statement-breakpoint
CREATE INDEX "idx_call_logs_conversation_id" ON "call_logs" USING btree ("conversation_id" uuid_ops);--> statement-breakpoint
CREATE INDEX "idx_call_logs_direction" ON "call_logs" USING btree ("direction" text_ops);--> statement-breakpoint
CREATE INDEX "idx_call_logs_restaurant_id" ON "call_logs" USING btree ("restaurant_id" uuid_ops);--> statement-breakpoint
CREATE INDEX "idx_password_reset_tokens_token" ON "password_reset_tokens" USING btree ("token" text_ops);--> statement-breakpoint
CREATE INDEX "idx_platform_settings_key" ON "platform_settings" USING btree ("key" text_ops);--> statement-breakpoint
CREATE INDEX "idx_notification_preferences_restaurant_id" ON "notification_preferences" USING btree ("restaurant_id" uuid_ops);--> statement-breakpoint
CREATE INDEX "idx_notification_preferences_user_id" ON "notification_preferences" USING btree ("user_id" uuid_ops);--> statement-breakpoint
CREATE INDEX "idx_message_templates_restaurant_id" ON "message_templates" USING btree ("restaurant_id" uuid_ops);--> statement-breakpoint
CREATE INDEX "idx_audit_logs_actor_type" ON "audit_logs" USING btree ("actor_type" text_ops);--> statement-breakpoint
CREATE INDEX "idx_audit_logs_created_at" ON "audit_logs" USING btree ("created_at" timestamptz_ops);--> statement-breakpoint
CREATE INDEX "idx_audit_logs_severity" ON "audit_logs" USING btree ("severity" text_ops);--> statement-breakpoint
CREATE INDEX "idx_menu_items_restaurant_id" ON "menu_items" USING btree ("restaurant_id" uuid_ops);--> statement-breakpoint
CREATE INDEX "idx_menu_modifier_groups_item_id" ON "menu_modifier_groups" USING btree ("menu_item_id" uuid_ops);--> statement-breakpoint
CREATE INDEX "idx_menu_modifier_groups_restaurant_id" ON "menu_modifier_groups" USING btree ("restaurant_id" uuid_ops);--> statement-breakpoint
CREATE INDEX "idx_menu_modifier_options_group_id" ON "menu_modifier_options" USING btree ("modifier_group_id" uuid_ops);--> statement-breakpoint
CREATE INDEX "idx_llm_models_provider_id" ON "llm_models" USING btree ("provider_id" uuid_ops);--> statement-breakpoint
CREATE INDEX "idx_voice_models_provider_id" ON "voice_models" USING btree ("voice_provider_id" uuid_ops);--> statement-breakpoint
CREATE INDEX "idx_restaurant_api_keys_restaurant" ON "restaurant_api_keys" USING btree ("restaurant_id" uuid_ops);--> statement-breakpoint
CREATE INDEX "idx_sip_registrations_aor" ON "sip_registrations" USING btree ("aor" text_ops);--> statement-breakpoint
CREATE UNIQUE INDEX "idx_sip_registrations_aor_contact" ON "sip_registrations" USING btree ("aor" text_ops,"contact_uri" text_ops);--> statement-breakpoint
CREATE INDEX "idx_sip_registrations_expires" ON "sip_registrations" USING btree ("expires_at" timestamptz_ops);--> statement-breakpoint
CREATE INDEX "idx_sip_registrations_restaurant" ON "sip_registrations" USING btree ("restaurant_id" uuid_ops);--> statement-breakpoint
CREATE INDEX "idx_sip_call_sessions_restaurant" ON "sip_call_sessions" USING btree ("restaurant_id" uuid_ops);--> statement-breakpoint
CREATE INDEX "idx_sip_call_sessions_started_at" ON "sip_call_sessions" USING btree ("started_at" timestamptz_ops);--> statement-breakpoint
CREATE INDEX "idx_sip_call_sessions_status" ON "sip_call_sessions" USING btree ("status" text_ops);--> statement-breakpoint
CREATE UNIQUE INDEX "idx_ai_agents_default_per_restaurant" ON "ai_agents" USING btree ("restaurant_id" uuid_ops) WHERE (is_default = true);--> statement-breakpoint
CREATE INDEX "idx_ai_agents_restaurant_id" ON "ai_agents" USING btree ("restaurant_id" uuid_ops);--> statement-breakpoint
CREATE INDEX "idx_ai_agents_role" ON "ai_agents" USING btree ("role" enum_ops);--> statement-breakpoint
CREATE INDEX "idx_roles_restaurant_id" ON "roles" USING btree ("restaurant_id" uuid_ops);--> statement-breakpoint
CREATE UNIQUE INDEX "idx_roles_restaurant_name" ON "roles" USING btree ("restaurant_id" text_ops,"name" text_ops);--> statement-breakpoint
CREATE INDEX "idx_alert_settings_restaurant_id" ON "alert_settings" USING btree ("restaurant_id" uuid_ops);--> statement-breakpoint
CREATE INDEX "idx_languages_code" ON "languages" USING btree ("code" text_ops);--> statement-breakpoint
CREATE INDEX "idx_languages_is_active" ON "languages" USING btree ("is_active" bool_ops);--> statement-breakpoint
CREATE INDEX "idx_translations_key_id" ON "translations" USING btree ("key_id" uuid_ops);--> statement-breakpoint
CREATE INDEX "idx_translations_lang_code" ON "translations" USING btree ("lang_code" text_ops);--> statement-breakpoint
CREATE INDEX "idx_translation_keys_category" ON "translation_keys" USING btree ("category" text_ops);--> statement-breakpoint
CREATE INDEX "idx_translation_keys_key" ON "translation_keys" USING btree ("key" text_ops);
*/