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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19eeb57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8e36dfa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee76c0c
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
import express from 'express';
import axios from 'axios';
import { authenticateApiKey, apiLimiter } from '../middleware/midware.js';
import { Federation } from '../models.js';
const FedsRoutes = express.Router();

/**
 * @swagger
 * /api/v2/federation/newfed:
 *   post:
 *     summary: Create a new federation
 *     tags: [Federation]
 *     description: Creates a new federation with a unique name and owner.
 *     security:
 *       - apiKeyAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *                 description: The unique name of the federation.
 *               owner:
 *                 type: integer
 *                 description: The user ID of the federation owner.
 *     parameters:
 *       - in: header
 *         name: x-api-key
 *         required: true
 *         description: API key for authentication.
 *         schema:
 *           type: string
 *     responses:
 *       200:
 *         description: Federation created successfully.
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: Federation created successfully
 *                 federation:
 *                   type: object
 *                   properties:
 *                     name:
 *                       type: string
 *                     owner:
 *                       type: integer
 *                     banned_users:
 *                       type: array
 *                       items:
 *                         type: integer
 *                     sub_federations:
 *                       type: array
 *                       items:
 *                         type: string
 *       400:
 *         description: Federation already exists or missing parameters.
 *       500:
 *         description: Internal server error.
 */
FedsRoutes.post("/api/v2/federation/newfed", authenticateApiKey, apiLimiter, async (req, res) => {
  try {
    const { name, owner } = req.body;
    
    const existing = await Federation.findOne({ name });
    if (existing) return res.status(200).json({ error: "Federation already exists" });

    const federation = new Federation({ name, owner, banned_users: [], sub_federations: [] });
    await federation.save();

    res.json({ message: "Federation created successfully", federation });
  } catch (err) {
    console.log(err.message);
    res.status(500).json({ error: err.message });
  }
});

/**
 * @swagger
 * /api/v2/federation/subfed:
 *   post:
 *     summary: Add a sub-federation
 *     tags: [Federation]
 *     description: Adds a federation as a sub-federation under a parent federation.
 *     security:
 *       - apiKeyAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               parent_uuid:
 *                 type: string
 *                 description: UUID of the parent federation.
 *               child_uuid:
 *                 type: string
 *                 description: UUID of the child federation to be added as a sub-federation.
 *     parameters:
 *       - in: header
 *         name: x-api-key
 *         required: true
 *         description: API key for authentication.
 *         schema:
 *           type: string
 *     responses:
 *       200:
 *         description: Sub-federation added successfully.
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "Federation ChildName is now a sub-federation of ParentName"
 *       404:
 *         description: Parent or child federation not found.
 *       500:
 *         description: Internal server error.
 */
FedsRoutes.post("/api/v2/federation/subfed", authenticateApiKey, apiLimiter, async (req, res) => {
  try {
    const { parent_uuid, child_uuid } = req.body;

    const parent = await Federation.findOne({ uuid: parent_uuid });
    const child = await Federation.findOne({ uuid: child_uuid });

    if (!parent || !child) return res.status(404).json({ error: "Federation not found" });

    if (!parent.sub_federations.includes(child.uuid)) {
      parent.sub_federations.push(child.uuid);
      await parent.save();
    }

    res.json({ message: `Federation ${child.name} is now a sub-federation of ${parent.name}` });
  } catch (err) {
    res.status(500).json({ error: err.message });
  }
});

/**
 * @swagger
 * /api/v2/federation/getfed/{uuid}:
 *   get:
 *     summary: Check Federation Info
 *     tags: [Federation]
 *     parameters:
 *       - in: path
 *         name: uuid
 *         required: true
 *         description: Check uuid to info
 *         schema:
 *           type: string
 *       - in: header
 *         name: x-api-key
 *         required: true
 *         description: API key for authentication
 *         schema:
 *           type: string
 *     responses:
 *       200:
 *         description: Returns
 */
FedsRoutes.get("/api/v2/federation/getfed/:uuid", authenticateApiKey, apiLimiter, async (req, res) => {
  try {
    const federation = await Federation.findOne({ uuid: req.params.uuid });
    
    if (!federation) return res.status(404).json({ error: "Federation not found" });

    const subFeds = await Federation.find({ uuid: { $in: federation.sub_federations } });

    res.json({ federation, sub_federations: subFeds });
  } catch (err) {
    res.status(500).json({ error: err.message });
  }
});

/**
 * @swagger
 * /api/v2/federation/unban:
 *   post:
 *     summary: Unban a user in a federation
 *     tags: [Federation]
 *     description: Removes a user from the banned list of a specified federation.
 *     security:
 *       - apiKeyAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *                 description: Name of the federation where the user will be unbanned.
 *               user_id:
 *                 type: integer
 *                 description: User ID to be unbanned.
 *     parameters:
 *       - in: header
 *         name: x-api-key
 *         required: true
 *         description: API key for authentication.
 *         schema:
 *           type: string
 *     responses:
 *       200:
 *         description: User unbanned successfully.
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 message:
 *                   type: string
 *                   example: "User 12345 unbanned from federation FederationName."
 *       400:
 *         description: Missing federation name or user ID.
 *       404:
 *         description: Federation not found.
 *       500:
 *         description: Internal server error.
 */
FedsRoutes.post('/api/v2/federation/unban', authenticateApiKey, async (req, res) => {
    try {
        const { name, user_id } = req.body;

        if (!name || !user_id || isNaN(user_id)) {
            return res.status(400).json({ error: "Federation name and valid user ID required" });
        }

        const federation = await Federation.findOne({ name });

        if (!federation) {
            return res.status(404).json({ error: "Federation not found." });
        }

        federation.banned_users = federation.banned_users.filter(id => Number(id) !== Number(user_id));
        await federation.save();

        await Federation.updateMany(
            { uuid: { $in: federation.sub_federations } },
            { $pull: { banned_users: Number(user_id) } }
        );

        res.json({ success: true, message: `User ${user_id} unbanned from ${name} and its sub-federations.` });
    } catch (error) {
        res.status(500).json({ error: `Failed to unban user: ${error.message}` });
    }
});

/**
 * @swagger
 * /api/v2/federation/ban:
 *   post:
 *     summary: Ban a user in a federation
 *     tags: [Federation]
 *     description: Bans a user in the specified federation and all its sub-federations.
 *     security:
 *       - apiKeyAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               federation_uuid:
 *                 type: string
 *                 description: UUID of the federation where the user will be banned.
 *               user_id:
 *                 type: integer
 *                 description: User ID to be banned.
 *     parameters:
 *       - in: header
 *         name: x-api-key
 *         required: true
 *         description: API key for authentication.
 *         schema:
 *           type: string
 *     responses:
 *       200:
 *         description: User banned successfully.
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "User 12345 banned in FederationName and all its sub-federations"
 *       404:
 *         description: Federation not found.
 *       500:
 *         description: Internal server error.
 */
FedsRoutes.post("/api/v2/federation/ban", authenticateApiKey, apiLimiter, async (req, res) => {
    try {
        const { federation_uuid, user_id } = req.body;

        if (!federation_uuid || !user_id || isNaN(user_id)) {
            return res.status(400).json({ error: "Federation UUID and valid user ID required" });
        }

        const federation = await Federation.findOne({ uuid: federation_uuid });

        if (!federation) return res.status(404).json({ error: "Federation not found" });

        if (!federation.banned_users.includes(Number(user_id))) {
            federation.banned_users.push(Number(user_id));
            await federation.save();
        }
  
        await Federation.updateMany(
            { uuid: { $in: federation.sub_federations } },
            { $addToSet: { banned_users: Number(user_id) } }
        );

        res.json({ message: `User ${user_id} banned in ${federation.name} and all its sub-federations.` });
        console.log(user_id);
    } catch (err) {
        res.status(500).json({ error: err.message });
    }
});

/**
 * @swagger
 * /api/v2/federation/ban-check:
 *   post:
 *     summary: Check if a user is banned in a federation
 *     tags: [Federation]
 *     description: Checks whether a user is banned in a specific federation using UUID and user ID.
 *     security:
 *       - apiKeyAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               federation_uuid:
 *                 type: string
 *                 description: UUID of the federation.
 *               user_id:
 *                 type: integer
 *                 description: User ID to check.
 *     parameters:
 *       - in: header
 *         name: x-api-key
 *         required: true
 *         description: API key for authentication.
 *         schema:
 *           type: string
 *     responses:
 *       200:
 *         description: Ban status of the user in the federation.
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 federation:
 *                   type: string
 *                   example: "AkenoX Federation"
 *                 user_id:
 *                   type: integer
 *                   example: 12345
 *                 is_banned:
 *                   type: boolean
 *                   example: true
 *       400:
 *         description: Missing federation UUID or invalid user ID.
 *       404:
 *         description: Federation not found.
 *       500:
 *         description: Internal server error.
 */
FedsRoutes.post("/api/v2/federation/ban-check", authenticateApiKey, async (req, res) => {
  try {
    const { federation_uuid, user_id } = req.body;

    if (!federation_uuid || !user_id || isNaN(user_id)) {
      return res.status(400).json({ error: "Federation UUID and valid user ID required." });
    }

    const federation = await Federation.findOne({ uuid: federation_uuid });

    if (!federation) {
      return res.status(404).json({ error: "Federation not found." });
    }

    const isBanned = federation.banned_users.includes(user_id);

    res.json({ 
      federation: federation.name, 
      user_id, 
      is_banned: isBanned 
    });

  } catch (error) {
    res.status(500).json({ error: `Failed to check ban status: ${error.message}` });
  }
});

/**
 * @swagger
 * /api/v2/federation/fedstats:
 *   get:
 *     summary: Get Federation Statistics
 *     tags: [Federation]
 *     description: Retrieve the statistics of a federation, including total banned users and sub-federations.
 *     parameters:
 *       - in: query
 *         name: uuid
 *         required: true
 *         description: UUID of the federation to check stats for.
 *         schema:
 *           type: string
 *       - in: header
 *         name: x-api-key
 *         required: true
 *         description: API key for authentication.
 *         schema:
 *           type: string
 *     responses:
 *       200:
 *         description: Federation statistics retrieved successfully.
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 stats:
 *                   type: object
 *                   properties:
 *                     federation_name:
 *                       type: string
 *                       example: "Example Federation"
 *                     total_banned_users:
 *                       type: integer
 *                       example: 10
 *                     total_sub_federations:
 *                       type: integer
 *                       example: 3
 *                     owner:
 *                       type: number
 *                       example: 123456789
 *       400:
 *         description: Missing or invalid federation UUID.
 *       404:
 *         description: Federation not found.
 *       500:
 *         description: Internal server error.
 */
FedsRoutes.get("/api/v2/federation/fedstats", authenticateApiKey, async (req, res) => {
  try {
    const { uuid } = req.query;

    if (!uuid) {
      return res.status(400).json({ error: "Federation UUID is required" });
    }

    const federation = await Federation.findOne({ uuid });

    if (!federation) {
      return res.status(404).json({ error: "Federation not found" });
    }

    const stats = {
      federation_name: federation.name,
      total_banned_users: federation.banned_users.length,
      total_sub_federations: federation.sub_federations.length,
      owner: federation.owner,
    };

    res.json({ success: true, stats });
  } catch (error) {
    res.status(500).json({ error: `Failed to fetch federation stats: ${error.message}` });
  }
});

/**
 * @swagger
 * /api/v2/federation/unsubfed:
 *   post:
 *     summary: Unsubscribe a Sub-Federation
 *     tags: [Federation]
 *     description: Remove a sub-federation from a parent federation.
 *     security:
 *       - apiKeyAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               parent_uuid:
 *                 type: string
 *                 description: UUID of the parent federation.
 *               child_uuid:
 *                 type: string
 *                 description: UUID of the sub-federation to be removed.
 *     parameters:
 *       - in: header
 *         name: x-api-key
 *         required: true
 *         description: API key for authentication.
 *         schema:
 *           type: string
 *     responses:
 *       200:
 *         description: Sub-federation successfully removed.
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "Federation SubFed has been unsubscribed from ParentFed."
 *       400:
 *         description: Missing or invalid federation UUIDs.
 *       404:
 *         description: Federation not found.
 *       500:
 *         description: Internal server error.
 */
FedsRoutes.post("/api/v2/federation/unsubfed", authenticateApiKey, apiLimiter, async (req, res) => {
  try {
    const { parent_uuid, child_uuid } = req.body;

    if (!parent_uuid || !child_uuid) {
      return res.status(400).json({ error: "Both parent and child federation UUIDs are required." });
    }

    const parent = await Federation.findOne({ uuid: parent_uuid });
    const child = await Federation.findOne({ uuid: child_uuid });

    if (!parent || !child) {
      return res.status(404).json({ error: "Federation not found." });
    }

    parent.sub_federations = parent.sub_federations.filter(uuid => uuid !== child_uuid);
    await parent.save();

    res.json({ message: `Federation ${child.name} has been unsubscribed from ${parent.name}` });
  } catch (error) {
    res.status(500).json({ error: `Failed to unsubscribe sub-federation: ${error.message}` });
  }
});

/**
 * @swagger
 * /api/v2/federation/renamefed:
 *   post:
 *     summary: Rename a Federation
 *     tags: [Federation]
 *     description: Change the name of an existing federation.
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               federation_uuid:
 *                 type: string
 *                 description: UUID of the federation to be renamed.
 *               new_name:
 *                 type: string
 *                 description: The new name for the federation.
 *     parameters:
 *       - in: header
 *         name: x-api-key
 *         required: true
 *         description: API key for authentication.
 *         schema:
 *           type: string
 *     responses:
 *       200:
 *         description: Federation successfully renamed.
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "Federation renamed to NewFedName successfully."
 *       400:
 *         description: Missing parameters or federation name already exists.
 *       404:
 *         description: Federation not found.
 *       500:
 *         description: Internal server error.
 */
FedsRoutes.post("/api/v2/federation/renamefed", authenticateApiKey, apiLimiter, async (req, res) => {
  try {
    const { federation_uuid, new_name } = req.body;

    if (!federation_uuid || !new_name) {
      return res.status(400).json({ error: "Federation UUID and new name are required." });
    }

    const federation = await Federation.findOne({ uuid: federation_uuid });

    if (!federation) {
      return res.status(404).json({ error: "Federation not found." });
    }

    const existingFederation = await Federation.findOne({ name: new_name });
    if (existingFederation) {
      return res.status(400).json({ error: "Federation name already exists." });
    }

    federation.name = new_name;
    await federation.save();

    res.json({ message: `Federation renamed to ${new_name} successfully.` });
  } catch (error) {
    res.status(500).json({ error: `Failed to rename federation: ${error.message}` });
  }
});

export { FedsRoutes };