// src/routes/admin.content.routes.test.ts
import { describe, it, expect, vi, beforeEach, afterAll } from 'vitest';
import supertest from 'supertest';
import type { Request, Response, NextFunction } from 'express';
import path from 'path';
import {
  createMockUserProfile,
  createMockSuggestedCorrection,
  createMockBrand,
  createMockRecipe,
  createMockFlyer,
  createMockRecipeComment,
  createMockUnmatchedFlyerItem,
} from '../tests/utils/mockFactories';
import type { SuggestedCorrection, Brand, UserProfile, UnmatchedFlyerItem } from '../types';
import { NotFoundError } from '../services/db/errors.db'; // This can stay, it's a type/class not a module with side effects.
import fs from 'node:fs/promises';
import { createTestApp } from '../tests/utils/createTestApp';
import { cleanupFiles } from '../tests/utils/cleanupFiles';

// Mock the file upload middleware to allow testing the controller's internal check
vi.mock('../middleware/fileUpload.middleware', () => ({
  requireFileUpload: () => (req: Request, res: Response, next: NextFunction) => next(),
}));

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, mockedBrandService } = vi.hoisted(() => {
  return {
    mockedDb: {
      adminRepo: {
        getSuggestedCorrections: vi.fn(),
        approveCorrection: vi.fn(),
        rejectCorrection: vi.fn(),
        updateSuggestedCorrection: vi.fn(),
        getUnmatchedFlyerItems: vi.fn(),
        getFlyersForReview: vi.fn(), // Added for flyer review tests
        updateRecipeStatus: vi.fn(),
        updateRecipeCommentStatus: vi.fn(),
        updateBrandLogo: vi.fn(),
        getApplicationStats: vi.fn(),
      },
      flyerRepo: {
        getAllBrands: vi.fn(),
        deleteFlyer: vi.fn(),
      },
      recipeRepo: {
        deleteRecipe: vi.fn(),
      },
      userRepo: {
        findUserProfileById: vi.fn(),
        deleteUserById: vi.fn(),
      },
    },
    mockedBrandService: {
      updateBrandLogo: 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),
  mkdir: 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),
    mkdir: vi.fn().mockResolvedValue(undefined),
  },
}));
vi.mock('../services/backgroundJobService');
vi.mock('../services/geocodingService.server');
vi.mock('../services/queueService.server');
vi.mock('../services/queues.server');
vi.mock('../services/workers.server');
vi.mock('../services/monitoringService.server');
vi.mock('../services/cacheService.server');
vi.mock('../services/userService');
vi.mock('../services/brandService', () => ({
  brandService: mockedBrandService,
}));
vi.mock('../services/receiptService.server');
vi.mock('../services/aiService.server');
vi.mock('../config/env', () => ({
  config: {
    database: { host: 'localhost', port: 5432, user: 'test', password: 'test', name: 'test' },
    redis: { url: 'redis://localhost:6379' },
    auth: { jwtSecret: 'test-secret' },
    server: { port: 3000, host: 'localhost' },
  },
  isAiConfigured: vi.fn().mockReturnValue(false),
  parseConfig: vi.fn(),
}));
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', async () => {
  const { mockLogger, createMockLogger } = await import('../tests/utils/mockLogger');
  return {
    logger: mockLogger,
    createScopedLogger: vi.fn(() => createMockLogger()),
  };
});

// Mock the passport middleware
// Note: admin.routes.ts imports from '../config/passport', so we mock that path
vi.mock('../config/passport', () => ({
  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.' });
  },
}));

// Import the router AFTER all mocks are defined.
import adminRouter from './admin.routes';

describe('Admin Content Management Routes (/api/admin)', () => {
  const adminUser = createMockUserProfile({
    role: 'admin',
    user: { user_id: 'admin-user-id', email: 'admin@test.com' },
  });
  // 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,
  });

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

  afterAll(async () => {
    // Safeguard to clean up any logo files created during tests.
    const uploadDir = path.resolve(__dirname, '../../../flyer-images');
    try {
      const allFiles = await fs.readdir(uploadDir);
      // Files are named like 'logoImage-timestamp-original.ext'
      const testFiles = allFiles
        .filter((f) => f.startsWith('logoImage-'))
        .map((f) => path.join(uploadDir, f));

      if (testFiles.length > 0) {
        await cleanupFiles(testFiles);
      }
    } catch (error) {
      if (error instanceof Error && (error as NodeJS.ErrnoException).code !== 'ENOENT') {
        console.error('Error during admin content test file cleanup:', error);
      }
    }
  });

  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.data).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.error.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.data).toEqual({ message: 'Correction approved successfully.' });
      expect(vi.mocked(mockedDb.adminRepo.approveCorrection)).toHaveBeenCalledWith(
        correctionId,
        expect.anything(),
      );
    });

    it('POST /corrections/:id/approve should return 500 on DB error', async () => {
      const correctionId = 123;
      vi.mocked(mockedDb.adminRepo.approveCorrection).mockRejectedValue(new Error('DB Error'));
      const response = await supertest(app).post(`/api/admin/corrections/${correctionId}/approve`);
      expect(response.status).toBe(500);
    });

    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.data).toEqual({ message: 'Correction rejected successfully.' });
    });

    it('POST /corrections/:id/reject should return 500 on DB error', async () => {
      const correctionId = 789;
      vi.mocked(mockedDb.adminRepo.rejectCorrection).mockRejectedValue(new Error('DB Error'));
      const response = await supertest(app).post(`/api/admin/corrections/${correctionId}/reject`);
      expect(response.status).toBe(500);
    });

    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.data).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.error.message).toBe('Correction with ID 999 not found');
    });

    it('PUT /corrections/:id should return 500 on a generic DB error', async () => {
      vi.mocked(mockedDb.adminRepo.updateSuggestedCorrection).mockRejectedValue(
        new Error('Generic DB Error'),
      );
      const response = await supertest(app)
        .put('/api/admin/corrections/101')
        .send({ suggested_value: 'new value' });
      expect(response.status).toBe(500);
      expect(response.body.error.message).toBe('Generic DB Error');
    });
  });

  describe('Flyer Review Routes', () => {
    it('GET /review/flyers should return flyers for review', async () => {
      const mockFlyers = [
        createMockFlyer({ flyer_id: 1, status: 'needs_review' }),
        createMockFlyer({ flyer_id: 2, status: 'needs_review' }),
      ];
      vi.mocked(mockedDb.adminRepo.getFlyersForReview).mockResolvedValue(mockFlyers);
      const response = await supertest(app).get('/api/admin/review/flyers');
      expect(response.status).toBe(200);
      expect(response.body.data).toEqual(mockFlyers);
      expect(vi.mocked(mockedDb.adminRepo.getFlyersForReview)).toHaveBeenCalledWith(
        expect.anything(),
      );
    });

    it('GET /review/flyers should return 500 on DB error', async () => {
      vi.mocked(mockedDb.adminRepo.getFlyersForReview).mockRejectedValue(new Error('DB Error'));
      const response = await supertest(app).get('/api/admin/review/flyers');
      expect(response.status).toBe(500);
      expect(response.body.error.message).toBe('DB Error');
    });
  });

  describe('Stats Routes', () => {
    // This test covers the error path for GET /stats
    it('GET /stats should return 500 on DB error', async () => {
      vi.mocked(mockedDb.adminRepo.getApplicationStats).mockRejectedValue(new Error('DB Error'));
      const response = await supertest(app).get('/api/admin/stats');
      expect(response.status).toBe(500);
      expect(response.body.error.message).toBe('DB Error');
    });
  });

  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.data).toEqual(mockBrands);
    });

    it('GET /brands should return 500 on DB error', async () => {
      vi.mocked(mockedDb.flyerRepo.getAllBrands).mockRejectedValue(new Error('DB Error'));
      const response = await supertest(app).get('/api/admin/brands');
      expect(response.status).toBe(500);
      expect(response.body.error.message).toBe('DB Error');
    });

    it('POST /brands/:id/logo should upload a logo and update the brand', async () => {
      const brandId = 55;
      const mockLogoUrl = '/flyer-images/brand-logos/test-logo.png';
      vi.mocked(mockedBrandService.updateBrandLogo).mockResolvedValue(mockLogoUrl);
      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.data.message).toBe('Brand logo updated successfully.');
      expect(vi.mocked(mockedBrandService.updateBrandLogo)).toHaveBeenCalledWith(
        brandId,
        expect.objectContaining({ fieldname: 'logoImage' }),
        expect.anything(),
      );
    });

    it('POST /brands/:id/logo should return 500 on DB error', async () => {
      const brandId = 55;
      vi.mocked(mockedBrandService.updateBrandLogo).mockRejectedValue(new Error('DB Error'));
      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(500);
    });

    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.error.message).toMatch(
        /Logo image file is required|The request data is invalid|Logo image file is missing./,
      );
    });

    it('should clean up the uploaded file if updating the brand logo fails', async () => {
      const brandId = 55;
      const dbError = new Error('DB Connection Failed');
      vi.mocked(mockedBrandService.updateBrandLogo).mockRejectedValue(dbError);

      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(500);
      // Verify that the cleanup function was called via the mocked fs module
      expect(fs.unlink).toHaveBeenCalledTimes(1);
      // The filename is predictable because of the multer config in admin.routes.ts
      expect(fs.unlink).toHaveBeenCalledWith(expect.stringContaining('logoImage-'));
    });

    it('POST /brands/:id/logo should return 400 if a non-image file is uploaded', async () => {
      const brandId = 55;
      const response = await supertest(app)
        .post(`/api/admin/brands/${brandId}/logo`)
        .attach('logoImage', Buffer.from('this is not an image'), 'document.txt');
      expect(response.status).toBe(400);
      // This message comes from the handleMulterError middleware for the imageFileFilter
      expect(response.body.error.message).toBe('Only image files are allowed!');
    });

    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('DELETE /recipes/:recipeId should delete a recipe', async () => {
      const recipeId = 300;
      vi.mocked(mockedDb.recipeRepo.deleteRecipe).mockResolvedValue(undefined);

      const response = await supertest(app).delete(`/api/admin/recipes/${recipeId}`);
      expect(response.status).toBe(204);
      expect(vi.mocked(mockedDb.recipeRepo.deleteRecipe)).toHaveBeenCalledWith(
        recipeId,
        expect.anything(),
        true,
        expect.anything(),
      );
    });

    it('DELETE /recipes/:recipeId should return 400 for invalid ID', async () => {
      const response = await supertest(app).delete('/api/admin/recipes/abc');
      expect(response.status).toBe(400);
    });

    it('DELETE /recipes/:recipeId should return 500 on DB error', async () => {
      const recipeId = 300;
      vi.mocked(mockedDb.recipeRepo.deleteRecipe).mockRejectedValue(new Error('DB Error'));
      const response = await supertest(app).delete(`/api/admin/recipes/${recipeId}`);
      expect(response.status).toBe(500);
    });

    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.data).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 /recipes/:id/status should return 500 on DB error', async () => {
      const recipeId = 201;
      const requestBody = { status: 'public' as const };
      vi.mocked(mockedDb.adminRepo.updateRecipeStatus).mockRejectedValue(new Error('DB Error'));
      const response = await supertest(app)
        .put(`/api/admin/recipes/${recipeId}/status`)
        .send(requestBody);
      expect(response.status).toBe(500);
    });

    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.data).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);
    });

    it('PUT /comments/:id/status should return 500 on DB error', async () => {
      const commentId = 301;
      const requestBody = { status: 'hidden' as const };
      vi.mocked(mockedDb.adminRepo.updateRecipeCommentStatus).mockRejectedValue(
        new Error('DB Error'),
      );
      const response = await supertest(app)
        .put(`/api/admin/comments/${commentId}/status`)
        .send(requestBody);
      expect(response.status).toBe(500);
    });
  });

  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.data).toEqual(mockUnmatchedItems);
    });

    it('GET /unmatched-items should return 500 on DB error', async () => {
      vi.mocked(mockedDb.adminRepo.getUnmatchedFlyerItems).mockRejectedValue(new Error('DB Error'));
      const response = await supertest(app).get('/api/admin/unmatched-items');
      expect(response.status).toBe(500);
    });
  });

  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.error.message).toBe('Flyer with ID 999 not found.');
    });

    it('DELETE /flyers/:flyerId should return 500 on a generic DB error', async () => {
      const flyerId = 42;
      vi.mocked(mockedDb.flyerRepo.deleteFlyer).mockRejectedValue(new Error('Generic DB Error'));
      const response = await supertest(app).delete(`/api/admin/flyers/${flyerId}`);
      expect(response.status).toBe(500);
      expect(response.body.error.message).toBe('Generic DB Error');
    });

    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.error.details[0].message).toMatch(/Expected number, received nan/i);
    });
  });
});
