-- CreateTable
CREATE TABLE "users" (
    "id" SERIAL NOT NULL,
    "name" TEXT,
    "email" TEXT NOT NULL,
    "password_hash" TEXT,
    "status" TEXT,
    "app_id" INTEGER,
    "user_type" TEXT,
    "timezone" TEXT,
    "created_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "stores" (
    "id" SERIAL NOT NULL,
    "user_id" INTEGER,
    "shopify_domain" TEXT,
    "shop_name" TEXT,
    "shop_owner" TEXT,
    "access_token" TEXT,
    "api_key" TEXT,
    "api_secret" TEXT,
    "plan_name" TEXT,
    "currency" TEXT,
    "timezone" TEXT,
    "country" TEXT,
    "status" TEXT,
    "created_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "roles" (
    "id" SERIAL NOT NULL,
    "store_id" INTEGER,
    "code" TEXT,
    "name" TEXT,
    "description" TEXT,
    "created_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "permissions" (
    "id" SERIAL NOT NULL,
    "code" TEXT NOT NULL,
    "name" TEXT,
    "description" TEXT,
    "category" TEXT,
    "created_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "role_permissions" (
    "id" SERIAL NOT NULL,
    "role_id" INTEGER,
    "permission_id" INTEGER,
    "storeId" INTEGER,
    "userId" INTEGER,
    "created_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "user_roles" (
    "id" SERIAL NOT NULL,
    "user_id" INTEGER,
    "role_id" INTEGER,
    "assigned_by" INTEGER,
    "assigned_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
    "expires_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "mobile_apps" (
    "id" SERIAL NOT NULL,
    "store_id" INTEGER,
    "platform" TEXT,
    "status" TEXT,
    "logs" TEXT,
    "build_url" TEXT,
    "bundle_id" TEXT,
    "version_name" TEXT,
    "triggered_by" INTEGER,
    "created_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3),
    "usersId" INTEGER,
    "logo" TEXT,

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

-- CreateTable
CREATE TABLE "app_metadata" (
    "id" SERIAL NOT NULL,
    "app_id" INTEGER,
    "app_name" TEXT,
    "logo_url" TEXT,
    "splash_url" TEXT,
    "primary_color" TEXT,
    "secondary_color" TEXT,
    "font_family" TEXT,
    "bundle_id_ios" TEXT,
    "package_android" TEXT,
    "version" TEXT,
    "created_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "theme_presets" (
    "id" SERIAL NOT NULL,
    "name" TEXT,
    "primary_color" TEXT,
    "secondary_color" TEXT,
    "font_family" TEXT,
    "background_color" TEXT,
    "preview_image" TEXT,
    "created_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "app_themes" (
    "id" SERIAL NOT NULL,
    "app_id" INTEGER,
    "theme_id" INTEGER,
    "created_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "master_templates" (
    "id" SERIAL NOT NULL,
    "name" TEXT,
    "category" TEXT,
    "preview_image" TEXT,
    "default_dsl" JSONB,
    "tags" TEXT[],
    "created_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "layouts" (
    "id" SERIAL NOT NULL,
    "template_id" INTEGER,
    "page_name" TEXT,
    "handle" TEXT,
    "current_version_id" INTEGER,
    "is_published" BOOLEAN DEFAULT false,
    "created_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3),
    "app_id" INTEGER NOT NULL,
    "store_id" INTEGER NOT NULL,

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

-- CreateTable
CREATE TABLE "layout_versions" (
    "id" SERIAL NOT NULL,
    "layout_id" INTEGER NOT NULL,
    "version_number" INTEGER NOT NULL,
    "dsl" JSONB NOT NULL,
    "metadata" JSONB,
    "published_by" INTEGER,
    "is_published" BOOLEAN NOT NULL DEFAULT false,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,
    "app_id" INTEGER NOT NULL,
    "store_id" INTEGER NOT NULL,
    "page_name" TEXT,

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

-- CreateTable
CREATE TABLE "components_library" (
    "id" SERIAL NOT NULL,
    "key" TEXT NOT NULL,
    "category" TEXT,
    "display_name" TEXT,
    "props_schema" JSONB,
    "default_props" JSONB,
    "preview_image" TEXT,
    "created_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "integration_providers" (
    "id" SERIAL NOT NULL,
    "code" TEXT NOT NULL,
    "name" TEXT,
    "docs_url" TEXT,
    "created_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "integration_accounts" (
    "id" SERIAL NOT NULL,
    "store_id" INTEGER,
    "provider_id" INTEGER,
    "account_name" TEXT,
    "credentials" JSONB,
    "config" JSONB,
    "status" TEXT,
    "connected_at" TIMESTAMP(3),
    "last_sync" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "pricing_plans" (
    "id" SERIAL NOT NULL,
    "name" TEXT,
    "monthly_price" DECIMAL(10,2),
    "annual_price" DECIMAL(10,2),
    "description" TEXT,
    "is_active" BOOLEAN DEFAULT true,
    "created_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "features" (
    "id" SERIAL NOT NULL,
    "code" TEXT NOT NULL,
    "name" TEXT,
    "description" TEXT,
    "category" TEXT,
    "created_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "plan_features" (
    "id" SERIAL NOT NULL,
    "plan_id" INTEGER,
    "feature_id" INTEGER,
    "is_included" BOOLEAN DEFAULT true,
    "limit_value" TEXT,
    "created_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "custom_plans" (
    "id" SERIAL NOT NULL,
    "store_id" INTEGER,
    "base_plan_id" INTEGER,
    "name" TEXT,
    "price_monthly" DECIMAL(10,2),
    "price_yearly" DECIMAL(10,2),
    "overrides" JSONB,
    "status" TEXT,
    "created_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "subscriptions" (
    "id" SERIAL NOT NULL,
    "store_id" INTEGER,
    "plan_id" INTEGER,
    "custom_plan_id" INTEGER,
    "stripe_subscription_id" TEXT,
    "start_date" TIMESTAMP(3),
    "end_date" TIMESTAMP(3),
    "status" TEXT,
    "cancel_at_period_end" BOOLEAN DEFAULT false,
    "created_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "invoices" (
    "id" SERIAL NOT NULL,
    "store_id" INTEGER,
    "subscription_id" INTEGER,
    "stripe_invoice_id" TEXT,
    "amount" DECIMAL(12,2),
    "currency" TEXT,
    "paid" BOOLEAN DEFAULT false,
    "invoice_url" TEXT,
    "created_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "billing_usage" (
    "id" SERIAL NOT NULL,
    "store_id" INTEGER,
    "feature_code" TEXT,
    "usage_amount" DECIMAL(18,4),
    "usage_date" TIMESTAMP(3),
    "metadata" JSONB,
    "created_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "app_sale_performance" (
    "id" SERIAL NOT NULL,
    "app_id" INTEGER,
    "total_orders" INTEGER,
    "total_revenue" DECIMAL(12,2),
    "captured_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "Notifications" (
    "id" SERIAL NOT NULL,
    "store_id" INTEGER,
    "user_id" INTEGER,
    "title" TEXT,
    "message" TEXT,
    "type" TEXT,
    "captured_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "Session" (
    "id" TEXT NOT NULL,
    "shop" TEXT NOT NULL,
    "state" TEXT NOT NULL,
    "isOnline" BOOLEAN NOT NULL,
    "scope" TEXT,
    "expires" TIMESTAMP(3),
    "accessToken" TEXT,
    "userId" TEXT,

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

-- CreateTable
CREATE TABLE "images" (
    "id" SERIAL NOT NULL,
    "store_id" INTEGER,
    "user_id" INTEGER,
    "image_name" TEXT,
    "image_url" TEXT,
    "file_type" TEXT,
    "file_size" DOUBLE PRECISION,
    "created_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "invites" (
    "id" SERIAL NOT NULL,
    "email" TEXT NOT NULL,
    "role" TEXT NOT NULL,
    "permissions" TEXT,
    "token" TEXT NOT NULL,
    "StoreId" INTEGER,
    "roleId" INTEGER,
    "permissionId" INTEGER,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "expiresAt" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "storetoken" (
    "id" SERIAL NOT NULL,
    "storename" TEXT NOT NULL,
    "token" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "campaigns" (
    "id" SERIAL NOT NULL,
    "name" TEXT,
    "title" TEXT NOT NULL,
    "message" TEXT NOT NULL,
    "target" TEXT NOT NULL,
    "send_type" TEXT NOT NULL,
    "deep_link" TEXT,
    "image" TEXT,
    "store_id" INTEGER,
    "schedule_date" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "fcmtoken" (
    "id" SERIAL NOT NULL,
    "userid" INTEGER,
    "appid" INTEGER,
    "token" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "campaign_logs" (
    "id" SERIAL NOT NULL,
    "campaign_id" INTEGER,
    "notification_sent" INTEGER DEFAULT 0,
    "delivered" INTEGER DEFAULT 0,
    "unsuccessful_delivered" INTEGER DEFAULT 0,
    "opened" INTEGER DEFAULT 0,
    "clicked_url" INTEGER DEFAULT 0,
    "created_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3),

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

-- CreateIndex
CREATE UNIQUE INDEX "users_email_key" ON "users"("email");

-- CreateIndex
CREATE UNIQUE INDEX "permissions_code_key" ON "permissions"("code");

-- CreateIndex
CREATE UNIQUE INDEX "layouts_template_id_key" ON "layouts"("template_id");

-- CreateIndex
CREATE UNIQUE INDEX "layout_versions_layout_id_version_number_key" ON "layout_versions"("layout_id", "version_number");

-- CreateIndex
CREATE UNIQUE INDEX "components_library_key_key" ON "components_library"("key");

-- CreateIndex
CREATE UNIQUE INDEX "integration_providers_code_key" ON "integration_providers"("code");

-- CreateIndex
CREATE UNIQUE INDEX "features_code_key" ON "features"("code");

-- CreateIndex
CREATE UNIQUE INDEX "invites_token_key" ON "invites"("token");

-- CreateIndex
CREATE UNIQUE INDEX "storetoken_storename_key" ON "storetoken"("storename");

-- AddForeignKey
ALTER TABLE "layout_versions" ADD CONSTRAINT "layout_versions_layout_id_fkey" FOREIGN KEY ("layout_id") REFERENCES "layouts"("template_id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "plan_features" ADD CONSTRAINT "plan_features_feature_id_fkey" FOREIGN KEY ("feature_id") REFERENCES "features"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "plan_features" ADD CONSTRAINT "plan_features_plan_id_fkey" FOREIGN KEY ("plan_id") REFERENCES "pricing_plans"("id") ON DELETE CASCADE ON UPDATE CASCADE;
