const HttpException = require('../../../utils/HttpException.js');

exports.validateStoreExecutivesHistoryRequest = (body) => {
    const { executive_name, contact, status, start_term, end_term } = body;

    const errors = {};

    // if (!executive_name || executive_name.trim() === '') {
    //     errors.executive_name = ['Executive name is required'];
    // }

    // if (!contact || contact.trim() === '') {
    //     errors.contact = ['Contact is required'];
    // }

    // if (!status || status.trim() === '') {
    //     errors.status = ['Status is required'];
    // } else if (!['active', 'inactive'].includes(status.trim().toLowerCase())) {
    //     errors.status = ['Status must be either "active" or "inactive"'];
    // }

    // if (!start_term || start_term.trim() === '') {
    //     errors.start_term = ['Start term date is required'];
    // } else if (isNaN(Date.parse(start_term))) {
    //     errors.start_term = ['Start term date must be a valid date'];
    // }

    // if (!end_term || end_term.trim() === '') {
    //     errors.end_term = ['End term expired date is required'];
    // } else if (isNaN(Date.parse(end_term))) {
    //     errors.end_term = ['End term expired date must be a valid date'];
    // }

    // if (
    //     !errors.start_term &&
    //     !errors.end_term &&
    //     new Date(end_term) <= new Date(start_term)
    // ) {
    //     errors.end_term = ['End term expired date must be after start term date'];
    // }

    if (!status || status.trim() === '') {
        errors.status = ['Status is required'];
    } else if (!['active', 'inactive'].includes(status.trim().toLowerCase())) {
        errors.status = ['Status must be either "active" or "inactive"'];
    }

    if (start_term && isNaN(Date.parse(start_term))) {
        errors.start_term = ['Start term date must be a valid date'];
    }

    if (end_term && isNaN(Date.parse(end_term))) {
        errors.end_term = ['End term expired date must be a valid date'];
    }

    if (
        start_term &&
        end_term &&
        !errors.start_term &&
        !errors.end_term &&
        new Date(end_term) <= new Date(start_term)
    ) {
        errors.end_term = ['End term expired date must be after start term date'];
    }

    if (Object.keys(errors).length > 0) {
        throw new HttpException(errors, 422);
    }

    return {
        executive_name: executive_name?.trim() || null,
        contact: contact?.trim() || null,
        status: status.trim().toLowerCase(),
        start_term: start_term ? new Date(start_term) : null,
        end_term: end_term ? new Date(end_term) : null,
    };
};