module.exports = {
  meta: {
    type: 'problem',
    docs: {
      description: 'Enforce that API services do not use repositories directly',
      category: 'Architecture',
      recommended: true,
    },
    fixable: null,
    schema: [],
    messages: {
      forbiddenRepository: 'API Services should not use repositories directly. Use Cases should handle data access. Forbidden use of "{{name}}".',
    },
  },

  create(context) {
    return {
      ClassDeclaration(node) {
        const isService = node.id.name.endsWith('Service');
        if (!isService) return;

        // Check constructor for injected repositories
        const constructor = node.body.body.find(m => m.kind === 'constructor');
        if (constructor) {
          constructor.value.params.forEach(param => {
            if (param.type === 'TSParameterProperty') {
              const typeAnnotation = param.parameter.typeAnnotation;
              if (typeAnnotation && typeAnnotation.typeAnnotation.type === 'TSTypeReference') {
                const typeName = typeAnnotation.typeAnnotation.typeName.name;
                if (typeName.endsWith('Repository')) {
                  context.report({
                    node: param,
                    messageId: 'forbiddenRepository',
                    data: {
                      name: typeName,
                    },
                  });
                }
              }
            }
          });
        }
      },
    };
  },
};
