-- =============================================================================
-- متجر النخبة الرقمي — قاعدة البيانات الكاملة (PostgreSQL)
-- شغّل هذا الملف في phpPgAdmin أو من ترمنال cPanel:
--   psql -U username -d database_name -f DEPLOY_SQL.sql
-- =============================================================================

-- ============================== 1) الجداول ==============================

CREATE TABLE IF NOT EXISTS "user_groups" (
  "id" serial PRIMARY KEY,
  "name" text NOT NULL,
  "color" text DEFAULT '#0d7a6f',
  "profit_percent" double precision DEFAULT 0 NOT NULL,
  "is_vip" boolean DEFAULT false NOT NULL,
  "is_default" boolean DEFAULT false NOT NULL,
  "created_at" timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE IF NOT EXISTS "categories" (
  "id" serial PRIMARY KEY,
  "name" text NOT NULL,
  "parent_id" integer,
  "image" text,
  "icon" text,
  "sort" integer DEFAULT 0 NOT NULL,
  "active" boolean DEFAULT true NOT NULL,
  "profit_percent" double precision DEFAULT 0 NOT NULL,
  "created_at" timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE IF NOT EXISTS "provider_services" (
  "id" serial PRIMARY KEY,
  "provider_id" integer NOT NULL REFERENCES "providers"("id"),
  "ext_id" text NOT NULL,
  "name" text NOT NULL,
  "category" text,
  "price" double precision DEFAULT 0 NOT NULL,
  "min" integer DEFAULT 1 NOT NULL,
  "max" integer DEFAULT 100000 NOT NULL
);
CREATE INDEX IF NOT EXISTS "psvc_provider_idx" ON "provider_services" ("provider_id");

CREATE TABLE IF NOT EXISTS "services" (
  "id" serial PRIMARY KEY,
  "name" text NOT NULL,
  "description" text,
  "category_id" integer REFERENCES "categories"("id"),
  "image" text,
  "kind" text DEFAULT 'internal' NOT NULL,
  "base_price" double precision DEFAULT 0 NOT NULL,
  "price" double precision DEFAULT 0 NOT NULL,
  "profit_percent" double precision DEFAULT 0 NOT NULL,
  "provider_id" integer REFERENCES "providers"("id"),
  "provider_service_id" text,
  "min_qty" integer DEFAULT 1 NOT NULL,
  "max_qty" integer DEFAULT 1000000 NOT NULL,
  "sort" integer DEFAULT 0 NOT NULL,
  "active" boolean DEFAULT true NOT NULL,
  "pinned" boolean DEFAULT false NOT NULL,
  "pinned_sort" integer DEFAULT 0 NOT NULL,
  "created_at" timestamp with time zone DEFAULT now() NOT NULL
);
CREATE INDEX IF NOT EXISTS "services_cat_idx" ON "services" ("category_id");
CREATE INDEX IF NOT EXISTS "services_provider_idx" ON "services" ("provider_id");

CREATE TABLE IF NOT EXISTS "inventory_items" (
  "id" serial PRIMARY KEY,
  "service_id" integer NOT NULL REFERENCES "services"("id"),
  "code" text NOT NULL,
  "status" text DEFAULT 'available' NOT NULL,
  "sold_order_id" integer,
  "created_at" timestamp with time zone DEFAULT now() NOT NULL
);
CREATE INDEX IF NOT EXISTS "inv_service_idx" ON "inventory_items" ("service_id");
CREATE INDEX IF NOT EXISTS "inv_status_idx" ON "inventory_items" ("status");

CREATE TABLE IF NOT EXISTS "currencies" (
  "id" serial PRIMARY KEY,
  "code" text NOT NULL,
  "name" text NOT NULL,
  "symbol" text NOT NULL,
  "rate" double precision DEFAULT 1 NOT NULL,
  "is_base" boolean DEFAULT false NOT NULL,
  "active" boolean DEFAULT true NOT NULL
);

CREATE TABLE IF NOT EXISTS "orders" (
  "id" serial PRIMARY KEY,
  "user_id" integer NOT NULL REFERENCES "users"("id"),
  "service_id" integer REFERENCES "services"("id"),
  "link" text,
  "qty" integer DEFAULT 1 NOT NULL,
  "charge" double precision DEFAULT 0 NOT NULL,
  "profit" double precision DEFAULT 0 NOT NULL,
  "currency_id" integer REFERENCES "currencies"("id"),
  "kind" text DEFAULT 'internal' NOT NULL,
  "provider_order_id" text,
  "status" text DEFAULT 'pending' NOT NULL,
  "note" text,
  "manager_note" text,
  "timeline" jsonb DEFAULT '[]'::jsonb,
  "created_at" timestamp with time zone DEFAULT now() NOT NULL,
  "updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
CREATE INDEX IF NOT EXISTS "orders_user_idx" ON "orders" ("user_id");
CREATE INDEX IF NOT EXISTS "orders_status_idx" ON "orders" ("status");
CREATE INDEX IF NOT EXISTS "orders_created_idx" ON "orders" ("created_at");

CREATE TABLE IF NOT EXISTS "deposits" (
  "id" serial PRIMARY KEY,
  "user_id" integer NOT NULL REFERENCES "users"("id"),
  "amount" double precision DEFAULT 0 NOT NULL,
  "method" text DEFAULT 'manual' NOT NULL,
  "bank_id" integer,
  "receipt" text,
  "reference" text,
  "status" text DEFAULT 'pending' NOT NULL,
  "note" text,
  "decided_at" timestamp with time zone,
  "created_at" timestamp with time zone DEFAULT now() NOT NULL
);
CREATE INDEX IF NOT EXISTS "deposits_status_idx" ON "deposits" ("status");

CREATE TABLE IF NOT EXISTS "transactions" (
  "id" serial PRIMARY KEY,
  "user_id" integer NOT NULL REFERENCES "users"("id"),
  "type" text NOT NULL,
  "amount" double precision DEFAULT 0 NOT NULL,
  "balance_after" double precision DEFAULT 0 NOT NULL,
  "ref" text,
  "note" text,
  "created_at" timestamp with time zone DEFAULT now() NOT NULL
);
CREATE INDEX IF NOT EXISTS "tx_user_idx" ON "transactions" ("user_id");

CREATE TABLE IF NOT EXISTS "transfers" (
  "id" serial PRIMARY KEY,
  "from_user_id" integer NOT NULL REFERENCES "users"("id"),
  "to_user_id" integer NOT NULL REFERENCES "users"("id"),
  "amount" double precision DEFAULT 0 NOT NULL,
  "status" text DEFAULT 'completed' NOT NULL,
  "note" text,
  "created_at" timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE IF NOT EXISTS "banks" (
  "id" serial PRIMARY KEY,
  "name" text NOT NULL,
  "holder" text,
  "number" text,
  "kind" text DEFAULT 'bank' NOT NULL,
  "image" text,
  "qr" text,
  "active" boolean DEFAULT true NOT NULL,
  "sort" integer DEFAULT 0 NOT NULL
);

CREATE TABLE IF NOT EXISTS "recharge_codes" (
  "id" serial PRIMARY KEY,
  "code" text NOT NULL,
  "amount" double precision DEFAULT 0 NOT NULL,
  "batch" text NOT NULL,
  "used_by_id" integer REFERENCES "users"("id"),
  "used_at" timestamp with time zone,
  "created_at" timestamp with time zone DEFAULT now() NOT NULL
);
CREATE UNIQUE INDEX IF NOT EXISTS "codes_code_idx" ON "recharge_codes" ("code");

CREATE TABLE IF NOT EXISTS "kyc_requests" (
  "id" serial PRIMARY KEY,
  "user_id" integer NOT NULL REFERENCES "users"("id"),
  "full_name" text NOT NULL,
  "doc_type" text DEFAULT 'id' NOT NULL,
  "doc_number" text,
  "doc_image" text,
  "selfie_image" text,
  "status" text DEFAULT 'pending' NOT NULL,
  "reviewed_by_id" integer,
  "review_note" text,
  "decided_at" timestamp with time zone,
  "created_at" timestamp with time zone DEFAULT now() NOT NULL
);
CREATE INDEX IF NOT EXISTS "kyc_status_idx" ON "kyc_requests" ("status");

CREATE TABLE IF NOT EXISTS "complaints" (
  "id" serial PRIMARY KEY,
  "user_id" integer NOT NULL REFERENCES "users"("id"),
  "order_id" integer REFERENCES "orders"("id"),
  "subject" text NOT NULL,
  "body" text,
  "status" text DEFAULT 'open' NOT NULL,
  "reply" text,
  "resolved_at" timestamp with time zone,
  "created_at" timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE IF NOT EXISTS "agents" (
  "id" serial PRIMARY KEY,
  "name" text NOT NULL,
  "phone" text,
  "email" text,
  "referral_code" text NOT NULL,
  "commission_percent" double precision DEFAULT 5 NOT NULL,
  "balance" double precision DEFAULT 0 NOT NULL,
  "active" boolean DEFAULT true NOT NULL,
  "listed" boolean DEFAULT true NOT NULL,
  "payment_account" text,
  "created_at" timestamp with time zone DEFAULT now() NOT NULL
);
CREATE UNIQUE INDEX IF NOT EXISTS "agents_code_idx" ON "agents" ("referral_code");

CREATE TABLE IF NOT EXISTS "countries" (
  "id" serial PRIMARY KEY,
  "name" text NOT NULL,
  "flag" text,
  "phone_code" text NOT NULL,
  "active" boolean DEFAULT true NOT NULL,
  "sort" integer DEFAULT 0 NOT NULL
);

CREATE TABLE IF NOT EXISTS "profit_rules" (
  "id" serial PRIMARY KEY,
  "scope" text NOT NULL,
  "category_id" integer,
  "service_id" integer,
  "group_id" integer,
  "user_id" integer,
  "profit_type" text DEFAULT 'percent' NOT NULL,
  "value" double precision DEFAULT 0 NOT NULL,
  "active" boolean DEFAULT true NOT NULL,
  "note" text,
  "created_at" timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE IF NOT EXISTS "notifications" (
  "id" serial PRIMARY KEY,
  "channel" text DEFAULT 'app' NOT NULL,
  "audience" text DEFAULT 'all' NOT NULL,
  "target_ids" jsonb DEFAULT '[]'::jsonb,
  "title" text NOT NULL,
  "body" text,
  "status" text DEFAULT 'sent' NOT NULL,
  "error" text,
  "created_at" timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE IF NOT EXISTS "email_logs" (
  "id" serial PRIMARY KEY,
  "to_addr" text NOT NULL,
  "subject" text NOT NULL,
  "status" text DEFAULT 'sent' NOT NULL,
  "error" text,
  "created_at" timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE IF NOT EXISTS "announcements" (
  "id" serial PRIMARY KEY,
  "title" text NOT NULL,
  "body" text,
  "audience" text DEFAULT 'all' NOT NULL,
  "target_ids" jsonb DEFAULT '[]'::jsonb,
  "active" boolean DEFAULT true NOT NULL,
  "created_at" timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE IF NOT EXISTS "banners" (
  "id" serial PRIMARY KEY,
  "image" text,
  "title" text,
  "link" text,
  "sort" integer DEFAULT 0 NOT NULL,
  "active" boolean DEFAULT true NOT NULL
);

CREATE TABLE IF NOT EXISTS "api_logs" (
  "id" serial PRIMARY KEY,
  "user_id" integer,
  "method" text NOT NULL,
  "path" text NOT NULL,
  "status" integer NOT NULL,
  "ip" text,
  "duration_ms" integer,
  "created_at" timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE IF NOT EXISTS "security_logs" (
  "id" serial PRIMARY KEY,
  "event" text NOT NULL,
  "actor" text,
  "ip" text,
  "meta" jsonb DEFAULT '{}'::jsonb,
  "created_at" timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE IF NOT EXISTS "audit_logs" (
  "id" serial PRIMARY KEY,
  "admin_id" integer,
  "admin_name" text,
  "action" text NOT NULL,
  "entity" text,
  "entity_id" text,
  "meta" jsonb DEFAULT '{}'::jsonb,
  "ip" text,
  "created_at" timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE IF NOT EXISTS "ip_blacklist" (
  "id" serial PRIMARY KEY,
  "ip" text NOT NULL,
  "reason" text,
  "created_by_id" integer,
  "created_at" timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE IF NOT EXISTS "competitions" (
  "id" serial PRIMARY KEY,
  "name" text NOT NULL,
  "kind" text DEFAULT 'purchase' NOT NULL,
  "prize" text,
  "starts_at" timestamp with time zone,
  "ends_at" timestamp with time zone,
  "active" boolean DEFAULT true NOT NULL,
  "created_at" timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE IF NOT EXISTS "competition_entries" (
  "id" serial PRIMARY KEY,
  "competition_id" integer NOT NULL REFERENCES "competitions"("id"),
  "user_id" integer NOT NULL REFERENCES "users"("id"),
  "points" double precision DEFAULT 0 NOT NULL,
  "detail" text,
  "created_at" timestamp with time zone DEFAULT now() NOT NULL
);
CREATE INDEX IF NOT EXISTS "entries_comp_idx" ON "competition_entries" ("competition_id");

CREATE TABLE IF NOT EXISTS "backups" (
  "id" serial PRIMARY KEY,
  "filename" text NOT NULL,
  "size_bytes" integer DEFAULT 0 NOT NULL,
  "kind" text DEFAULT 'manual' NOT NULL,
  "sent_to_telegram" boolean DEFAULT false NOT NULL,
  "created_at" timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE IF NOT EXISTS "assistant_messages" (
  "id" serial PRIMARY KEY,
  "user_id" integer,
  "role" text DEFAULT 'user' NOT NULL,
  "content" text NOT NULL,
  "created_at" timestamp with time zone DEFAULT now() NOT NULL
);

-- المجموعات الافتراضية
INSERT INTO "user_groups" ("name", "color", "profit_percent", "is_default") VALUES
  ('العملاء العاديون', '#0d7a6f', 10, true),
  ('كبار العملاء VIP', '#e89a2b', 5, false),
  ('الموزعون', '#3b82c4', 3, false)
ON CONFLICT DO NOTHING;

-- العملة الأساسية
INSERT INTO "currencies" ("code", "name", "symbol", "rate", "is_base", "active") VALUES
  ('USD', 'دولار أمريكي', '$', 1, true, true),
  ('TRY', 'ليرة تركية', '₺', 38.5, false, true),
  ('SYP', 'ليرة سورية', 'ل.س', 13200, false, true)
ON CONFLICT DO NOTHING;

-- التصنيفات الأساسية
INSERT INTO "categories" ("name", "icon", "sort", "profit_percent") VALUES
  ('سوشيال ميديا', '📱', 1, 10),
  ('ألعاب وبطاقات', '🎮', 2, 12),
  ('اشتراكات رقمية', '💎', 3, 8)
ON CONFLICT DO NOTHING;

-- طرق الدفع الافتراضية
INSERT INTO "banks" ("name", "holder", "number", "kind", "sort", "active") VALUES
  ('شام كاش', 'إدارة المتجر', '0931 000 000', 'sham', 1, true),
  ('سيريتل كاش', 'إدارة المتجر', '0944 000 000', 'syriatel', 2, true),
  ('USDT (TRC20)', 'Treasury', 'TXyZ...', 'usdt', 3, true)
ON CONFLICT DO NOTHING;

-- =============================================================================
-- انتهى الملف. الآن يمكنك تسجيل الدخول بـ:
--   المستخدم: admin
--   كلمة المرور: admin123
-- =============================================================================
