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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/**
 * ADMIN LEARNING — the missing dashboard control surfaces
 *
 *   CATEGORIES   — full CRUD (previously impossible from the dashboard: the
 *                  table only had rows from old manual seeds, and course
 *                  creation REQUIRES a categoryId → a new category meant SQL).
 *   QUIZ         — per-lesson question bank CRUD (previously NO write path at
 *                  all: certificates require a ≥70% quiz average, yet questions
 *                  could only ever come from an old manual seed).
 *   ENROLLMENTS  — per-course roster: list/search, manual grant, revoke,
 *                  reactivate (previously an admin could not even SEE who is
 *                  enrolled, only approve/reject payments).
 *
 * All procedures are admin-gated (adminQuery / adminMutation → requireAuth +
 * requireRole("admin") + globalRateLimit) and invalidate the course caches so
 * the public site reflects changes immediately.
 */

import { categories, courses, enrollments, lessons, quizQuestions, users } from "@db/schema";
import { TRPCError } from "@trpc/server";
import { and, asc, count, desc, eq, like, max, or, sql } from "drizzle-orm";
import { z } from "zod";
import { invalidateCourseCache, invalidateStatsCache } from "./lib/cache.js";
import { adminMutation, adminQuery } from "./middleware";
import { getDb } from "./queries/connection";

// ═══════════════════════════════════════════════════════════════════════════
// CATEGORIES — full CRUD
// ═══════════════════════════════════════════════════════════════════════════

const SLUG_RE = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;

export const categoryProcedures = {
  /** Admin list WITH usage counts (courses per category) — drives the UI. */
  adminCategories: adminQuery.query(async () => {
    const db = getDb();
    const rows = await db
      .select({
        id: categories.id,
        slug: categories.slug,
        nameEn: categories.nameEn,
        nameAr: categories.nameAr,
        descriptionEn: categories.descriptionEn,
        descriptionAr: categories.descriptionAr,
        icon: categories.icon,
        sortOrder: categories.sortOrder,
        courseCount: count(courses.id),
      })
      .from(categories)
      .leftJoin(courses, eq(courses.categoryId, categories.id))
      .groupBy(categories.id)
      .orderBy(asc(categories.sortOrder), asc(categories.id));
    return { items: rows };
  }),

  createCategory: adminMutation
    .input(z.object({
      slug: z.string().min(2).max(200).regex(SLUG_RE, "Slug must be lowercase kebab-case"),
      nameEn: z.string().min(1).max(255),
      nameAr: z.string().min(1).max(255),
      descriptionEn: z.string().max(2000).optional(),
      descriptionAr: z.string().max(2000).optional(),
      icon: z.string().max(100).default("book"),
      sortOrder: z.number().int().default(0),
    }))
    .mutation(async ({ input }) => {
      const db = getDb();
      const [existing] = await db.select({ id: categories.id }).from(categories).where(eq(categories.slug, input.slug)).limit(1);
      if (existing) throw new TRPCError({ code: "CONFLICT", message: "A category with this slug already exists" });
      const [created] = await db.insert(categories).values({
        slug: input.slug,
        nameEn: input.nameEn,
        nameAr: input.nameAr,
        descriptionEn: input.descriptionEn ?? null,
        descriptionAr: input.descriptionAr ?? null,
        icon: input.icon || "book",
        sortOrder: input.sortOrder,
      }).$returningId();
      await invalidateCourseCache();
      return { success: true, id: created?.id };
    }),

  updateCategory: adminMutation
    .input(z.object({
      id: z.number().int().positive(),
      nameEn: z.string().min(1).max(255).optional(),
      nameAr: z.string().min(1).max(255).optional(),
      descriptionEn: z.string().max(2000).nullable().optional(),
      descriptionAr: z.string().max(2000).nullable().optional(),
      icon: z.string().max(100).optional(),
      sortOrder: z.number().int().optional(),
    }))
    .mutation(async ({ input }) => {
      const db = getDb();
      const { id, ...updates } = input;
      const clean = Object.fromEntries(Object.entries(updates).filter(([, v]) => v !== undefined));
      if (Object.keys(clean).length === 0) return { success: true };
      await db.update(categories).set(clean).where(eq(categories.id, id));
      await invalidateCourseCache();
      return { success: true };
    }),

  /**
   * Delete a category. Guard: categories with courses are REJECTED (courses
   * FK is RESTRICT) — the admin must reassign those courses first; the error
   * says exactly how many courses block the deletion.
   */
  deleteCategory: adminMutation
    .input(z.object({ id: z.number().int().positive() }))
    .mutation(async ({ input }) => {
      const db = getDb();
      const [{ value: courseCount }] = await db
        .select({ value: count() })
        .from(courses)
        .where(eq(courses.categoryId, input.id));
      if (courseCount > 0) {
        throw new TRPCError({
          code: "CONFLICT",
          message: `This category still has ${courseCount} course(s) — reassign them to another category first`,
        });
      }
      await db.delete(categories).where(eq(categories.id, input.id));
      await invalidateCourseCache();
      return { success: true };
    }),
};

// ═══════════════════════════════════════════════════════════════════════════
// QUIZ — per-lesson question bank CRUD (admin side; the student side never
// receives correctIndex — see quiz-router.ts)
// ═══════════════════════════════════════════════════════════════════════════

const optionsSchema = z.array(z.string().min(1).max(500)).min(2).max(6);

export const quizAdminProcedures = {
  /** Full question list for a lesson INCLUDING correctIndex (admin-only view). */
  adminQuizByLesson: adminQuery
    .input(z.object({ lessonId: z.number().int().positive() }))
    .query(async ({ input }) => {
      const db = getDb();
      const items = await db
        .select()
        .from(quizQuestions)
        .where(eq(quizQuestions.lessonId, input.lessonId))
        .orderBy(asc(quizQuestions.sortOrder), asc(quizQuestions.id));
      return { items };
    }),

  createQuizQuestion: adminMutation
    .input(z.object({
      lessonId: z.number().int().positive(),
      questionEn: z.string().min(3).max(2000),
      questionAr: z.string().min(3).max(2000),
      optionsEn: optionsSchema,
      optionsAr: optionsSchema,
      correctIndex: z.number().int().min(0).max(5),
      points: z.number().int().min(1).max(100).default(10),
    }))
    .mutation(async ({ input }) => {
      const db = getDb();
      if (input.correctIndex >= input.optionsEn.length || input.correctIndex >= input.optionsAr.length) {
        throw new TRPCError({ code: "BAD_REQUEST", message: "correctIndex is out of range for the given options" });
      }
      const [lesson] = await db.select({ id: lessons.id }).from(lessons).where(eq(lessons.id, input.lessonId)).limit(1);
      if (!lesson) throw new TRPCError({ code: "NOT_FOUND", message: "Lesson not found" });

      const [{ maxSort }] = await db
        .select({ maxSort: max(quizQuestions.sortOrder) })
        .from(quizQuestions)
        .where(eq(quizQuestions.lessonId, input.lessonId));

      const [created] = await db.insert(quizQuestions).values({
        lessonId: input.lessonId,
        questionEn: input.questionEn,
        questionAr: input.questionAr,
        optionsEn: input.optionsEn,
        optionsAr: input.optionsAr,
        correctIndex: input.correctIndex,
        points: input.points,
        sortOrder: (maxSort ?? 0) + 1,
      }).$returningId();
      return { success: true, id: created?.id };
    }),

  updateQuizQuestion: adminMutation
    .input(z.object({
      id: z.number().int().positive(),
      questionEn: z.string().min(3).max(2000).optional(),
      questionAr: z.string().min(3).max(2000).optional(),
      optionsEn: optionsSchema.optional(),
      optionsAr: optionsSchema.optional(),
      correctIndex: z.number().int().min(0).max(5).optional(),
      points: z.number().int().min(1).max(100).optional(),
      sortOrder: z.number().int().min(0).optional(),
    }))
    .mutation(async ({ input }) => {
      const db = getDb();
      const { id, ...updates } = input;
      const clean = Object.fromEntries(Object.entries(updates).filter(([, v]) => v !== undefined));
      if (Object.keys(clean).length === 0) return { success: true };

      // Cross-validate correctIndex against the EFFECTIVE options (new or stored).
      if (clean.correctIndex !== undefined) {
        const [row] = await db
          .select({ optionsEn: quizQuestions.optionsEn })
          .from(quizQuestions)
          .where(eq(quizQuestions.id, id))
          .limit(1);
        if (!row) throw new TRPCError({ code: "NOT_FOUND", message: "Question not found" });
        const effectiveOptions = (clean.optionsEn as string[] | undefined) ?? row.optionsEn ?? [];
        if (Number(clean.correctIndex) >= effectiveOptions.length) {
          throw new TRPCError({ code: "BAD_REQUEST", message: "correctIndex is out of range for the options" });
        }
      }

      await db.update(quizQuestions).set(clean).where(eq(quizQuestions.id, id));
      return { success: true };
    }),

  deleteQuizQuestion: adminMutation
    .input(z.object({ id: z.number().int().positive() }))
    .mutation(async ({ input }) => {
      const db = getDb();
      await db.delete(quizQuestions).where(eq(quizQuestions.id, input.id));
      return { success: true };
    }),

  // ─── Quiz storage self-test (admin ops diagnostic) ───
  // Runs the FULL write path (INSERT → SELECT → DELETE) against quizQuestions
  // with a canary row and reports the RAW database error per step. Admin quiz
  // quiz paths surfaced generic 500s while the student read path worked —
  // this probe returns the underlying driver message instead of guessing.
  quizStorageSelfTest: adminMutation
    .mutation(async () => {
      const db = getDb();
      // mysql2's real cause (code/errno/sqlMessage) is nested inside drizzle's
      // "Failed query" wrapper — surface the whole chain, not just the wrapper.
      const describe = (err: unknown): string => {
        const e = err as { message?: string; cause?: { code?: string; errno?: number; sqlMessage?: string; sqlState?: string } };
        const parts = [e?.message ?? String(err)];
        if (e?.cause) {
          parts.push(`code=${e.cause.code ?? "?"}`, `errno=${e.cause.errno ?? "?"}`, `sqlState=${e.cause.sqlState ?? "?"}`, `sqlMessage=${e.cause.sqlMessage ?? "?"}`);
        }
        return parts.filter(Boolean).join(" | ").slice(0, 600);
      };
      const steps: Array<{ step: string; ok: boolean; error?: string; id?: number }> = [];
      // Canary needs a REAL lesson (FK target) — pick the smallest lesson id.
      const [lesson] = await db.select({ id: lessons.id }).from(lessons).orderBy(asc(lessons.id)).limit(1);
      if (!lesson) {
        return { ok: false, steps: [{ step: "lesson-lookup", ok: false, error: "no lessons exist — cannot run the quiz storage self-test" }] };
      }
      let canaryId: number | undefined;
      try {
        const [created] = await db.insert(quizQuestions).values({
          lessonId: lesson.id,
          questionEn: "SELFTEST canary — safe to delete",
          questionAr: "اختبار ذاتي — يُحذف فورًا",
          optionsEn: ["a", "b", "c", "d"],
          optionsAr: ["أ", "ب", "ج", "د"],
          correctIndex: 0,
          points: 1,
          sortOrder: 9999,
        }).$returningId();
        canaryId = (created as { id?: number })?.id;
        steps.push({ step: "insert", ok: true, id: canaryId });
      } catch (err) {
        // biome-ignore lint/correctness/usePlaywrightValidDescribeCallback: NOT Playwright — `describe` is the local error formatter above (line 238) for the DB canary diagnostics
        steps.push({ step: "insert", ok: false, error: describe(err) });
      }
      if (canaryId !== undefined) {
        try {
          const rows = await db.select().from(quizQuestions).where(eq(quizQuestions.id, canaryId)).limit(1);
          steps.push({ step: "select", ok: rows.length === 1 });
        } catch (err) {
          // biome-ignore lint/correctness/usePlaywrightValidDescribeCallback: NOT Playwright — local error formatter, not a test runner describe
          steps.push({ step: "select", ok: false, error: describe(err) });
        }
        try {
          await db.delete(quizQuestions).where(eq(quizQuestions.id, canaryId));
          steps.push({ step: "delete", ok: true });
        } catch (err) {
          // biome-ignore lint/correctness/usePlaywrightValidDescribeCallback: NOT Playwright — local error formatter, not a test runner describe
          steps.push({ step: "delete", ok: false, error: describe(err) });
        }
      }
      return { ok: steps.every((s) => s.ok), steps };
    }),
};

// ═══════════════════════════════════════════════════════════════════════════
// ENROLLMENTS — per-course roster management
// ═══════════════════════════════════════════════════════════════════════════

export const enrollmentAdminProcedures = {
  /** Roster of one course: student identity + status + progress, newest first. */
  courseEnrollments: adminQuery
    .input(z.object({
      courseId: z.number().int().positive(),
      status: z.enum(["all", "active", "completed", "refunded", "cancelled"]).default("all"),
      search: z.string().max(100).default(""),
      limit: z.number().int().min(1).max(100).default(50),
    }))
    .query(async ({ input }) => {
      const db = getDb();
      const conditions = [eq(enrollments.courseId, input.courseId)];
      if (input.status !== "all") conditions.push(eq(enrollments.status, input.status));
      const search = input.search.trim();
      if (search) {
        const escaped = search.replace(/[%_]/g, (m) => `\\${m}`);
        const pattern = `%${escaped}%`;
        const match = or(
          like(users.email, pattern),
          like(users.username, pattern),
          like(users.name, pattern),
        );
        if (match) conditions.push(match);
      }

      const rows = await db
        .select({
          id: enrollments.id,
          userId: enrollments.userId,
          username: users.username,
          email: users.email,
          fullName: users.name,
          status: enrollments.status,
          progress: enrollments.progress,
          isCompleted: enrollments.isCompleted,
          enrolledAt: enrollments.enrolledAt,
          completedAt: enrollments.completedAt,
        })
        .from(enrollments)
        .innerJoin(users, eq(users.id, enrollments.userId))
        .where(and(...conditions))
        .orderBy(desc(enrollments.enrolledAt))
        .limit(input.limit);

      const [{ value: total }] = await db
        .select({ value: count() })
        .from(enrollments)
        .innerJoin(users, eq(users.id, enrollments.userId))
        .where(and(...conditions));

      return { items: rows, total: total ?? 0 };
    }),

  /**
   * Manual grant — e.g. a student paid via a channel outside the platform.
   * Creates (or reactivates) the enrollment row + bumps studentCount.
   * Idempotent: an existing ACTIVE enrollment short-circuits with a clear code.
   */
  grantEnrollment: adminMutation
    .input(z.object({
      courseId: z.number().int().positive(),
      userId: z.number().int().positive(),
    }))
    .mutation(async ({ input }) => {
      const db = getDb();
      const [course] = await db.select({ id: courses.id, slug: courses.slug }).from(courses).where(eq(courses.id, input.courseId)).limit(1);
      if (!course) throw new TRPCError({ code: "NOT_FOUND", message: "Course not found" });
      const [user] = await db.select({ id: users.id }).from(users).where(eq(users.id, input.userId)).limit(1);
      if (!user) throw new TRPCError({ code: "NOT_FOUND", message: "User not found" });

      const [existing] = await db
        .select({ id: enrollments.id, status: enrollments.status })
        .from(enrollments)
        .where(and(eq(enrollments.courseId, input.courseId), eq(enrollments.userId, input.userId)))
        .limit(1);

      if (existing?.status === "active") {
        return { success: true, alreadyActive: true };
      }

      if (existing) {
        // refunded / cancelled / completed → reactivate in place (unique index
        // on (userId, courseId) forbids a second row).
        await db.update(enrollments)
          .set({ status: "active", enrolledAt: new Date() })
          .where(eq(enrollments.id, existing.id));
      } else {
        await db.insert(enrollments).values({
          userId: input.userId,
          courseId: input.courseId,
          status: "active",
        });
        await db.update(courses)
          .set({ studentCount: sql`${courses.studentCount} + 1` })
          .where(eq(courses.id, input.courseId));
      }
      await invalidateCourseCache();
      await invalidateStatsCache();
      return { success: true, alreadyActive: false };
    }),

  /**
   * Revoke — sets status="cancelled" (keeps the row + history; a revoke is
   * NOT a refund) and decrements studentCount. Idempotent.
   */
  revokeEnrollment: adminMutation
    .input(z.object({ enrollmentId: z.number().int().positive() }))
    .mutation(async ({ input }) => {
      const db = getDb();
      const [row] = await db
        .select({ id: enrollments.id, courseId: enrollments.courseId, status: enrollments.status })
        .from(enrollments)
        .where(eq(enrollments.id, input.enrollmentId))
        .limit(1);
      if (!row) throw new TRPCError({ code: "NOT_FOUND", message: "Enrollment not found" });

      if (row.status !== "cancelled") {
        await db.update(enrollments)
          .set({ status: "cancelled" })
          .where(eq(enrollments.id, row.id));
        await db.update(courses)
          .set({ studentCount: sql`GREATEST(${courses.studentCount} - 1, 0)` })
          .where(eq(courses.id, row.courseId));
        await invalidateCourseCache();
        await invalidateStatsCache();
      }
      return { success: true };
    }),

  /** Reactivate a cancelled/refunded enrollment WITHOUT double-counting. */
  reactivateEnrollment: adminMutation
    .input(z.object({ enrollmentId: z.number().int().positive() }))
    .mutation(async ({ input }) => {
      const db = getDb();
      const [row] = await db
        .select({ id: enrollments.id, courseId: enrollments.courseId, status: enrollments.status })
        .from(enrollments)
        .where(eq(enrollments.id, input.enrollmentId))
        .limit(1);
      if (!row) throw new TRPCError({ code: "NOT_FOUND", message: "Enrollment not found" });

      if (row.status !== "active") {
        await db.update(enrollments)
          .set({ status: "active" })
          .where(eq(enrollments.id, row.id));
        await db.update(courses)
          .set({ studentCount: sql`${courses.studentCount} + 1` })
          .where(eq(courses.id, row.courseId));
        await invalidateCourseCache();
        await invalidateStatsCache();
      }
      return { success: true };
    }),

  /** Search users by email/username for the grant dialog (no role leak). */
  searchUsers: adminQuery
    .input(z.object({ q: z.string().min(2).max(100) }))
    .query(async ({ input }) => {
      const db = getDb();
      const escaped = input.q.trim().replace(/[%_]/g, (m) => `\\${m}`);
      const pattern = `%${escaped}%`;
      const rows = await db
        .select({ id: users.id, username: users.username, email: users.email, fullName: users.name, role: users.role })
        .from(users)
        .where(or(like(users.email, pattern), like(users.username, pattern), like(users.name, pattern)))
        .orderBy(asc(users.id))
        .limit(10);
      return { items: rows };
    }),

  // Used by the roster UI badge: which statuses exist for this course.
  enrollmentCounts: adminQuery
    .input(z.object({ courseId: z.number().int().positive() }))
    .query(async ({ input }) => {
      const db = getDb();
      const rows = await db
        .select({ status: enrollments.status, value: count() })
        .from(enrollments)
        .where(eq(enrollments.courseId, input.courseId))
        .groupBy(enrollments.status);
      const counts: Record<string, number> = { all: 0 };
      for (const r of rows) {
        counts[r.status] = r.value;
        counts.all = (counts.all ?? 0) + r.value;
      }
      return counts;
    }),
};