// src/routes/admin.content.routes.test.ts
import { describe, it, expect, vi, beforeEach } from 'vitest';
import supertest from 'supertest';
import { Request, Response, NextFunction } from 'express';
import adminRouter from './admin.routes'; // This was a duplicate, fixed.
import { createMockUserProfile, createMockSuggestedCorrection, createMockBrand, createMockRecipe, createMockRecipeComment, createMockUnmatchedFlyerItem } from '../tests/utils/mockFactories';
import { SuggestedCorrection, Brand, UserProfile, UnmatchedFlyerItem } from '../types';
import { NotFoundError } from '../services/db/errors.db';
import { createTestApp } from '../tests/utils/createTestApp';

const { mockLogger } = vi.hoisted(() => ({
  mockLogger: {
    info: vi.fn(),
    debug: vi.fn(),
    warn: vi.fn(),
    error: vi.fn(),
    child: vi.fn().mockReturnThis(),
  },
}));

vi.mock('../lib/queue', () => ({
  serverAdapter: {
    getRouter: () => (req: Request, res: Response, next: NextFunction) => next(), // Return a dummy express handler
  },
  // Mock other exports if needed
  emailQueue: {},
  cleanupQueue: {},
}));

const { mockedDb } = vi.hoisted(() => {
  return {
    mockedDb: {
      adminRepo: {
        getSuggestedCorrections: vi.fn(),
        approveCorrection: vi.fn(),
        rejectCorrection: vi.fn(),
        updateSuggestedCorrection: vi.fn(),
        getUnmatchedFlyerItems: vi.fn(),
        updateRecipeStatus: vi.fn(),
        updateRecipeCommentStatus: vi.fn(),
        updateBrandLogo: vi.fn(),
      },
      flyerRepo: {
        getAllBrands: vi.fn(),
        deleteFlyer: vi.fn(),
      },
      recipeRepo: {
        deleteRecipe: vi.fn(),
      },
      userRepo: {
        findUserProfileById: vi.fn(),
        deleteUserById: vi.fn(),
      },
    }
  }
});

vi.mock('../services/db/index.db', () => ({
  adminRepo: mockedDb.adminRepo,
  flyerRepo: mockedDb.flyerRepo,
  recipeRepo: mockedDb.recipeRepo,
  userRepo: mockedDb.userRepo,
  personalizationRepo: {},
  notificationRepo: {},
}));

// Mock other dependencies
vi.mock('../services/db/recipe.db');
vi.mock('../services/db/user.db');
vi.mock('node:fs/promises', () => ({
  // Named exports
  writeFile: vi.fn().mockResolvedValue(undefined),
  unlink: vi.fn().mockResolvedValue(undefined),
  // FIX: Add default export to handle `import fs from ...` syntax.
  default: {
    writeFile: vi.fn().mockResolvedValue(undefined),
    unlink: vi.fn().mockResolvedValue(undefined),
  },
}));
vi.mock('../services/backgroundJobService');
vi.mock('../services/geocodingService.server');
vi.mock('../services/queueService.server');
vi.mock('@bull-board/api'); // Keep this mock for the API part
vi.mock('@bull-board/api/bullMQAdapter'); // Keep this mock for the adapter

// Fix: Mock ExpressAdapter as a class to allow `new ExpressAdapter()` to work.
vi.mock('@bull-board/express', () => ({
  ExpressAdapter: class {
    setBasePath = vi.fn();
    getRouter = vi.fn().mockReturnValue((req: Request, res: Response, next: NextFunction) => next());
  },
}));

// Mock the logger
vi.mock('../services/logger.server', () => ({
  logger: mockLogger,
}));

// Mock the passport middleware
vi.mock('./passport.routes', () => ({
  default: {
    authenticate: vi.fn(() => (req: Request, res: Response, next: NextFunction) => {
      if (!req.user) return res.status(401).json({ message: 'Unauthorized' });
      next();
    }),
  },
  isAdmin: (req: Request, res: Response, next: NextFunction) => {
    const user = req.user as UserProfile | undefined;
    if (user && user.role === 'admin') next();
    else res.status(403).json({ message: 'Forbidden: Administrator access required.' });
  },
}));

describe('Admin Content Management Routes (/api/admin)', () => {
  const adminUser = createMockUserProfile({ role: 'admin', user_id: 'admin-user-id' });
  // Create a single app instance with an admin user for all tests in this suite.
  const app = createTestApp({ router: adminRouter, basePath: '/api/admin', authenticatedUser: adminUser });

  // Add a basic error handler to capture errors passed to next(err) and return JSON.
  // This prevents unhandled error crashes in tests and ensures we get the 500 response we expect.
  app.use((err: any, req: any, res: any, next: any) => {
    res.status(err.status || 500).json({ message: err.message, errors: err.errors });
  });

  beforeEach(() => {
    vi.clearAllMocks();
  });

  describe('Corrections Routes', () => {
    it('GET /corrections should return corrections data', async () => {
      const mockCorrections: SuggestedCorrection[] = [createMockSuggestedCorrection({ suggested_correction_id: 1 })];
      vi.mocked(mockedDb.adminRepo.getSuggestedCorrections).mockResolvedValue(mockCorrections);
      const response = await supertest(app).get('/api/admin/corrections');
      expect(response.status).toBe(200);
      expect(response.body).toEqual(mockCorrections);
    });

    it('should return 500 if the database call fails', async () => {
      vi.mocked(mockedDb.adminRepo.getSuggestedCorrections).mockRejectedValue(new Error('DB Error'));
      const response = await supertest(app).get('/api/admin/corrections');
      expect(response.status).toBe(500);
      expect(response.body.message).toBe('DB Error');
    });

    it('POST /corrections/:id/approve should approve a correction', async () => {
      const correctionId = 123;
      vi.mocked(mockedDb.adminRepo.approveCorrection).mockResolvedValue(undefined);
      const response = await supertest(app).post(`/api/admin/corrections/${correctionId}/approve`);
      expect(response.status).toBe(200);
      expect(response.body).toEqual({ message: 'Correction approved successfully.' });
      expect(vi.mocked(mockedDb.adminRepo.approveCorrection)).toHaveBeenCalledWith(correctionId, expect.anything());
    });

    it('POST /corrections/:id/reject should reject a correction', async () => {
      const correctionId = 789;
      vi.mocked(mockedDb.adminRepo.rejectCorrection).mockResolvedValue(undefined);
      const response = await supertest(app).post(`/api/admin/corrections/${correctionId}/reject`);
      expect(response.status).toBe(200);
      expect(response.body).toEqual({ message: 'Correction rejected successfully.' });
    });

    it('PUT /corrections/:id should update a correction', async () => {
      const correctionId = 101;
      const requestBody = { suggested_value: 'A new corrected value' };
      const mockUpdatedCorrection = createMockSuggestedCorrection({ suggested_correction_id: correctionId, ...requestBody });
      vi.mocked(mockedDb.adminRepo.updateSuggestedCorrection).mockResolvedValue(mockUpdatedCorrection);
      const response = await supertest(app).put(`/api/admin/corrections/${correctionId}`).send(requestBody);
      expect(response.status).toBe(200);
      expect(response.body).toEqual(mockUpdatedCorrection);
    });

    it('PUT /corrections/:id should return 400 for invalid data', async () => {
      const response = await supertest(app)
        .put('/api/admin/corrections/101')
        .send({ suggested_value: '' }); // Send empty value
      expect(response.status).toBe(400);
    });

    it('PUT /corrections/:id should return 404 if correction not found', async () => {
      vi.mocked(mockedDb.adminRepo.updateSuggestedCorrection).mockRejectedValue(new NotFoundError('Correction with ID 999 not found'));
      const response = await supertest(app).put('/api/admin/corrections/999').send({ suggested_value: 'new value' });
      expect(response.status).toBe(404);
      expect(response.body.message).toBe('Correction with ID 999 not found');
    });
  });

  describe('Brand Routes', () => {
    it('GET /brands should return a list of all brands', async () => {
      const mockBrands: Brand[] = [createMockBrand({ brand_id: 1, name: 'Brand A' })];
      vi.mocked(mockedDb.flyerRepo.getAllBrands).mockResolvedValue(mockBrands);
      const response = await supertest(app).get('/api/admin/brands');
      expect(response.status).toBe(200);
      expect(response.body).toEqual(mockBrands);
    });

    it('POST /brands/:id/logo should upload a logo and update the brand', async () => {
      const brandId = 55;
      vi.mocked(mockedDb.adminRepo.updateBrandLogo).mockResolvedValue(undefined);
      const response = await supertest(app)
        .post(`/api/admin/brands/${brandId}/logo`)
        .attach('logoImage', Buffer.from('dummy-logo-content'), 'test-logo.png');
      expect(response.status).toBe(200);
      expect(response.body.message).toBe('Brand logo updated successfully.');
      expect(vi.mocked(mockedDb.adminRepo.updateBrandLogo)).toHaveBeenCalledWith(brandId, expect.stringContaining('/assets/'), expect.anything());
    });

    it('POST /brands/:id/logo should return 400 if no file is uploaded', async () => {
      const response = await supertest(app).post('/api/admin/brands/55/logo');
      expect(response.status).toBe(400);
      expect(response.body.message).toMatch(/Logo image file is required|The request data is invalid/);
    });

    it('POST /brands/:id/logo should return 400 for an invalid brand ID', async () => {
      const response = await supertest(app).post('/api/admin/brands/abc/logo')
        .attach('logoImage', Buffer.from('dummy-logo-content'), 'test-logo.png');
      expect(response.status).toBe(400);
    });
  });

  describe('Recipe and Comment Routes', () => {
    it('PUT /recipes/:id/status should update a recipe status', async () => {
      const recipeId = 201;
      const requestBody = { status: 'public' as const };
      const mockUpdatedRecipe = createMockRecipe({ recipe_id: recipeId, status: 'public' });
      vi.mocked(mockedDb.adminRepo.updateRecipeStatus).mockResolvedValue(mockUpdatedRecipe);
      const response = await supertest(app).put(`/api/admin/recipes/${recipeId}/status`).send(requestBody);
      expect(response.status).toBe(200);
      expect(response.body).toEqual(mockUpdatedRecipe);
    });

    it('PUT /recipes/:id/status should return 400 for an invalid status value', async () => {
      const recipeId = 201;
      const requestBody = { status: 'invalid_status' };
      const response = await supertest(app).put(`/api/admin/recipes/${recipeId}/status`).send(requestBody);
      expect(response.status).toBe(400);
    });

    it('PUT /comments/:id/status should update a comment status', async () => {
      const commentId = 301;
      const requestBody = { status: 'hidden' as const };
      const mockUpdatedComment = createMockRecipeComment({ recipe_comment_id: commentId, status: 'hidden' }); // This was a duplicate, fixed.
      vi.mocked(mockedDb.adminRepo.updateRecipeCommentStatus).mockResolvedValue(mockUpdatedComment);
      const response = await supertest(app).put(`/api/admin/comments/${commentId}/status`).send(requestBody);
      expect(response.status).toBe(200);
      expect(response.body).toEqual(mockUpdatedComment);
    });

    it('PUT /comments/:id/status should return 400 for an invalid status value', async () => {
      const commentId = 301;
      const requestBody = { status: 'invalid_status' };
      const response = await supertest(app).put(`/api/admin/comments/${commentId}/status`).send(requestBody);
      expect(response.status).toBe(400);
    });

  });

  describe('Unmatched Items Route', () => {
    it('GET /unmatched-items should return a list of unmatched items', async () => {
      // Correctly create a mock for UnmatchedFlyerItem.
      const mockUnmatchedItems: UnmatchedFlyerItem[] = [createMockUnmatchedFlyerItem({ unmatched_flyer_item_id: 1, flyer_item_name: 'Mystery Item' })];
      vi.mocked(mockedDb.adminRepo.getUnmatchedFlyerItems).mockResolvedValue(mockUnmatchedItems);
      const response = await supertest(app).get('/api/admin/unmatched-items');
      expect(response.status).toBe(200);
      expect(response.body).toEqual(mockUnmatchedItems);
    });
  });

  describe('Flyer Routes', () => {
    it('DELETE /flyers/:flyerId should delete a flyer', async () => {
      const flyerId = 42;
      vi.mocked(mockedDb.flyerRepo.deleteFlyer).mockResolvedValue(undefined);

      const response = await supertest(app).delete(`/api/admin/flyers/${flyerId}`);
      expect(response.status).toBe(204);
      expect(vi.mocked(mockedDb.flyerRepo.deleteFlyer)).toHaveBeenCalledWith(flyerId, expect.anything());
    });

    it('DELETE /flyers/:flyerId should return 404 if flyer not found', async () => {
      const flyerId = 999;
      vi.mocked(mockedDb.flyerRepo.deleteFlyer).mockRejectedValue(new NotFoundError('Flyer with ID 999 not found.'));
      const response = await supertest(app).delete(`/api/admin/flyers/${flyerId}`);
      expect(response.status).toBe(404);
      expect(response.body.message).toBe('Flyer with ID 999 not found.');
    });

    it('DELETE /flyers/:flyerId should return 400 for an invalid flyerId', async () => {
      const response = await supertest(app).delete('/api/admin/flyers/abc');
      expect(response.status).toBe(400);
      expect(response.body.errors[0].message).toMatch(/Expected number, received nan/i);
    });
  });
});