import { BadRequestError, ErrorCodes } from '@humand-packages/common';
import { BulkReviewCategorizedHoursVC } from '../validationClasses/hourCategorization/bulkReviewCategorizedHoursVC';

/**
 * Cross-field shape rules for bulk-review input. Cannot live as a single class-validator
 * decorator cleanly because the `categorizedHourIds`-required-when-not-allRows rule needs
 * to fire when the field is missing — `skipMissingProperties: true` in the body validator
 * skips property validators on undefined values. Helper runs in the controller, before the
 * service is called.
 *
 * Throws BadRequestError naming the actually-wrong field in the message.
 */
export function validateBulkReviewShape(params: BulkReviewCategorizedHoursVC): void {
    if (params.allRows) {
        if (params.categorizedHourIds && params.categorizedHourIds.length > 0) {
            throw new BadRequestError('categorizedHourIds must be empty when allRows=true', ErrorCodes.BAD_REQUEST);
        }
        if (!params.filters) {
            throw new BadRequestError('filters is required when allRows=true', ErrorCodes.BAD_REQUEST);
        }
        return;
    }
    if (!params.categorizedHourIds || params.categorizedHourIds.length === 0) {
        throw new BadRequestError('categorizedHourIds is required when allRows is not true', ErrorCodes.BAD_REQUEST);
    }
}
