import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import Fastify from 'fastify';
import { fastifyAwilixPlugin } from '@fastify/awilix';
import { serializerCompiler, validatorCompiler } from 'fastify-type-provider-zod';
import { MealPlanStatus } from '@meshitrack/shared';

vi.mock('jose', () => ({
  createRemoteJWKSet: vi.fn(() => 'mock-jwks'),
  jwtVerify: vi.fn().mockResolvedValue({
    payload: {
      sub: 'kc-1',
      email: 'test@example.com',
      preferred_username: 'testuser',
      realm_access: { roles: ['member'] },
      householdIds: ['hh1'],
    },
    protectedHeader: { alg: 'RS256' },
    key: {},
  }),
}));

const {
  mockFindByHousehold,
  mockFindById,
  mockFindByWeek,
  mockCreate,
  mockUpdate,
  mockUpdateStatus,
  mockDelete,
} = vi.hoisted(() => ({
  mockFindByHousehold: vi.fn(),
  mockFindById: vi.fn(),
  mockFindByWeek: vi.fn(),
  mockCreate: vi.fn(),
  mockUpdate: vi.fn(),
  mockUpdateStatus: vi.fn(),
  mockDelete: vi.fn(),
}));

vi.mock('../../../src/modules/meal-plans/meal-plans.repository.js', () => ({
  MealPlanRepository: class {
    findByHousehold = mockFindByHousehold;
    findById = mockFindById;
    findByWeek = mockFindByWeek;
    create = mockCreate;
    update = mockUpdate;
    updateStatus = mockUpdateStatus;
    delete = mockDelete;
  },
}));

// Mock prerequisite repositories to allow SuggestionEngine/Gap to resolve
vi.mock('../../../src/modules/recipes/recipes.repository.js', () => ({
  RecipesRepository: class {
    findByHousehold = vi.fn().mockResolvedValue({ data: [] });
    findById = vi.fn();
  },
}));

vi.mock('../../../src/modules/pantry/pantry.repository.js', () => ({
  PantryRepository: class {
    findActiveByHousehold = vi.fn().mockResolvedValue([]);
  },
}));

vi.mock('../../../src/modules/nutrition-targets/nutrition-target.repository.js', () => ({
  NutritionTargetRepository: class {
    findByUser = vi.fn().mockResolvedValue(null);
  },
}));

vi.mock('../../../src/modules/products/products.repository.js', () => ({
  ProductsRepository: class {
    findByIds = vi.fn().mockResolvedValue([]);
  },
}));

vi.mock('../../../src/modules/users/users.repository.js', () => ({
  UsersRepository: class {
    findByKeycloakId = vi.fn().mockResolvedValue({ householdIds: ['hh1'] });
    upsertFromToken = vi.fn().mockResolvedValue({ householdIds: ['hh1'] });
  },
}));

import authPlugin from '../../../src/plugins/auth.plugin.js';
import householdPlugin from '../../../src/plugins/household.plugin.js';
import usersRoutes from '../../../src/modules/users/users.routes.js';
import mealPlanRoutes from '../../../src/modules/meal-plans/meal-plans.routes.js';

const emptyNutrition = { calories: 0, protein: 0, carbs: 0, fat: 0, fiber: 0, sugar: 0, sodium: 0, saturatedFat: 0, cholesterol: 0 };

function makePlan(overrides = {}) {
  return {
    _id: 'plan-1',
    householdId: 'hh1',
    weekStartDate: '2026-05-10',
    days: Array.from({ length: 7 }, (_, i) => ({
      date: `2026-05-${10 + i}`,
      meals: [],
      dailyNutritionTotal: emptyNutrition,
    })),
    status: MealPlanStatus.DRAFT,
    createdBy: 'kc-1',
    createdAt: new Date().toISOString(),
    updatedAt: new Date().toISOString(),
    ...overrides,
  };
}

describe('meal-plan.routes', () => {
  let app: any;

  async function buildTestApp() {
    const instance = Fastify({ logger: false });
    instance.setValidatorCompiler(validatorCompiler);
    instance.setSerializerCompiler(serializerCompiler);
    await instance.register(fastifyAwilixPlugin, {
      disposeOnClose: true,
      disposeOnResponse: true,
      strictBooleanEnforced: true,
    });
    await instance.register(authPlugin);
    await instance.register(householdPlugin);
    await instance.register(usersRoutes);
    await instance.register(mealPlanRoutes);
    await instance.ready();
    return instance;
  }

  const authHeaders = { authorization: 'Bearer valid-token' };

  beforeEach(async () => {
    vi.clearAllMocks();
    app = await buildTestApp();
  });

  afterEach(async () => {
    if (app) await app.close();
  });

  describe('GET /api/v1/households/:householdId/meal-plans', () => {
    it('returns paginated results', async () => {
      mockFindByHousehold.mockResolvedValue({
        data: [makePlan()],
        pagination: { cursor: null, hasMore: false },
      });

      const res = await app.inject({
        method: 'GET',
        url: '/api/v1/households/hh1/meal-plans',
        headers: authHeaders,
      });

      expect(res.statusCode).toBe(200);
      const body = res.json();
      expect(body.data).toHaveLength(1);
      expect(body.data[0]._id).toBe('plan-1');
    });
  });

  describe('GET /api/v1/households/:householdId/meal-plans/week/:weekStartDate', () => {
    it('returns matched weekly plan', async () => {
      mockFindByWeek.mockResolvedValue(makePlan());

      const res = await app.inject({
        method: 'GET',
        url: '/api/v1/households/hh1/meal-plans/week/2026-05-10',
        headers: authHeaders,
      });

      expect(res.statusCode).toBe(200);
      expect(res.json()._id).toBe('plan-1');
    });

    it('returns not-found message structure if missing', async () => {
      mockFindByWeek.mockResolvedValue(null);

      const res = await app.inject({
        method: 'GET',
        url: '/api/v1/households/hh1/meal-plans/week/2026-05-10',
        headers: authHeaders,
      });

      expect(res.statusCode).toBe(200);
      expect(res.json().message).toBe('No meal plan scheduled for this week');
    });
  });

  describe('POST /api/v1/households/:householdId/meal-plans', () => {
    it('creates a new plan', async () => {
      mockFindByWeek.mockResolvedValue(null);
      mockCreate.mockImplementation((data) => Promise.resolve({ ...data, _id: 'new-plan-id', createdAt: new Date(), updatedAt: new Date() }));

      const res = await app.inject({
        method: 'POST',
        url: '/api/v1/households/hh1/meal-plans',
        headers: { ...authHeaders, 'content-type': 'application/json' },
        body: JSON.stringify({
          weekStartDate: '2026-05-10',
          days: Array.from({ length: 7 }, (_, i) => ({
            date: `2026-05-${10 + i}`,
            meals: [],
            dailyNutritionTotal: emptyNutrition,
          })),
          status: 'draft',
        }),
      });

      expect(res.statusCode).toBe(201);
      const body = res.json();
      expect(body._id).toBe('new-plan-id');
      expect(body.status).toBe('draft');
    });
  });

  describe('GET /api/v1/households/:householdId/meal-plans/suggestions', () => {
    it('returns list of scored recipe recommendations', async () => {
      const res = await app.inject({
        method: 'GET',
        url: '/api/v1/households/hh1/meal-plans/suggestions?limit=2',
        headers: authHeaders,
      });

      expect(res.statusCode).toBe(200);
      const body = res.json();
      expect(Array.isArray(body)).toBe(true);
    });
  });

  describe('GET /api/v1/households/:householdId/meal-plans/:id/gap', () => {
    it('returns missing elements report', async () => {
      mockFindById.mockResolvedValue(makePlan());

      const res = await app.inject({
        method: 'GET',
        url: '/api/v1/households/hh1/meal-plans/plan-1/gap',
        headers: authHeaders,
      });

      expect(res.statusCode).toBe(200);
      const body = res.json();
      expect(body.mealPlanId).toBe('plan-1');
      expect(Array.isArray(body.missingItems)).toBe(true);
    });
  });

  describe('GET /api/v1/households/:householdId/meal-plans/:id', () => {
    it('returns plan if found', async () => {
      const planWithMeal = makePlan({
        createdAt: new Date(),
        days: [{
          date: '2026-05-10',
          meals: [{
            id: '123e4567-e89b-42d3-a456-426614174000',
            type: 'dinner',
            recipeId: 'recipe-1',
            recipeName: 'Spaghetti',
            servings: 2,
            perServingNutrition: emptyNutrition,
            customName: 'My Pasta',
            customNutrition: emptyNutrition,
            notes: 'Very yummy',
          }],
          dailyNutritionTotal: emptyNutrition,
        }]
      });
      mockFindById.mockResolvedValue(planWithMeal);

      const res = await app.inject({
        method: 'GET',
        url: '/api/v1/households/hh1/meal-plans/plan-1',
        headers: authHeaders,
      });

      expect(res.statusCode).toBe(200);
      expect(res.json()._id).toBe('plan-1');
      expect(res.json().days[0].meals).toHaveLength(1);
    });
  });

  describe('PATCH /api/v1/households/:householdId/meal-plans/:id', () => {
    it('updates plan content and returns it', async () => {
      mockUpdate.mockResolvedValue(makePlan({ status: MealPlanStatus.ACTIVE }));

      const res = await app.inject({
        method: 'PATCH',
        url: '/api/v1/households/hh1/meal-plans/plan-1',
        headers: { ...authHeaders, 'content-type': 'application/json' },
        body: JSON.stringify({
          status: MealPlanStatus.ACTIVE,
        }),
      });

      expect(res.statusCode).toBe(200);
      expect(res.json().status).toBe(MealPlanStatus.ACTIVE);
    });
  });

  describe('PATCH /api/v1/households/:householdId/meal-plans/:id/status', () => {
    it('updates plan status directly and returns it', async () => {
      mockUpdateStatus.mockResolvedValue(makePlan({ status: MealPlanStatus.ACTIVE }));

      const res = await app.inject({
        method: 'PATCH',
        url: '/api/v1/households/hh1/meal-plans/plan-1/status',
        headers: { ...authHeaders, 'content-type': 'application/json' },
        body: JSON.stringify({
          status: MealPlanStatus.ACTIVE,
        }),
      });

      expect(res.statusCode).toBe(200);
      expect(res.json().status).toBe(MealPlanStatus.ACTIVE);
    });
  });

  describe('DELETE /api/v1/households/:householdId/meal-plans/:id', () => {
    it('deletes the plan and returns 204', async () => {
      mockDelete.mockResolvedValue(makePlan());

      const res = await app.inject({
        method: 'DELETE',
        url: '/api/v1/households/hh1/meal-plans/plan-1',
        headers: authHeaders,
      });

      expect(res.statusCode).toBe(204);
    });
  });
});
