const { Op } = require("sequelize");
const dbSAPPS = require("../../helpers/db");
require("dotenv").config();

module.exports = {
  getAll,
  filter,
  option,
  count,
  create,
  update,
  enabled,
  delete: _delete,
};

async function getAll() {
  try {
    const clientTypes = await dbSAPPS.ClientType.findAll({
      order: [["modified", "DESC"]],
      include: [
        {
          model: dbSAPPS.Company,
          attributes: ["id", "display_name"],
          as: "company",
        },
        {
          model: dbSAPPS.Specialist,
          as: "specialists",
          include: [
            {
              model: dbSAPPS.SubSpecialist,
              as: 'subspecialists'
            }
          ]
        },
        {
          model: dbSAPPS.ClientSubtype,
          as: "clientsubtypes",
        },
      ],
  });

    return {
      clientTypes,
      success: true,
      message: "Client types loaded successfully!",
    };
  } catch (error) {
    const errorMessage = error.message || "Failed to load Client types!";
    return {
      success: false,
      message: errorMessage,
    };
  }
}

async function filter(params) {
  const filterConditions = {};

  if (params.display_name) {
    filterConditions.display_name = {
      [Op.like]: `%${params.display_name}%`,
    };
  }

  if (params.company_id) {
    filterConditions.company_id = {
      [Op.like]: `%${params.company_id}%`,
    };
  }

  if (params.code) {
    filterConditions.code = {
      [Op.like]: `%${params.code}%`,
    };
  }

  if (params.enabled) {
    filterConditions.enabled = {
      [Op.eq]: params.enabled === "1" ? true : false,
    };
  }

  try {
    const clientTypes = await dbSAPPS.ClientType.findAll({
      where: filterConditions,
      order: [["modified", "DESC"]], // Opsional: Mengurutkan hasil berdasarkan kolom 'modified'
      include: [
        {
          model: dbSAPPS.Company,
          as: "company",
          attributes: ["id", "display_name"],
        },
        {
          model: dbSAPPS.Specialist,
          as: "specialists",
          include: [
            {
              model: dbSAPPS.SubSpecialist,
              as: 'subspecialists'
            }
          ]
        },
        {
          model: dbSAPPS.ClientSubtype,
          as: "clientsubtypes",
        },
      ],
    });

    return {
      success: true,
      clientTypes,
      message: "Client Types loaded successfully!",
    };
  } catch (error) {
    console.error("Error retrieving Client Types: ", error);
    const errorMessage =
      error.message ||
      "Failed to retrieve Client Types. Please try again later.";
    return {
      success: false,
      message: errorMessage,
    };
  }
}

async function option() {
  try {
    const clientTypes = await dbSAPPS.ClientType.findAll({
      attributes: ["id", "display_name", "code", "company_id"],
      where: { enabled: true },
      order: [["display_name", "ASC"]],
      include: [
        {
          model: dbSAPPS.Company,
          as: "company",
          attributes: ["id", "display_name"],
        },
        {
          model: dbSAPPS.Specialist,
          as: "specialists",
          attributes: ["id", "display_name", "code"],
          include: [
            {
              model: dbSAPPS.SubSpecialist,
              as: 'subspecialists',
              attributes: ["id", "display_name", "code"],
            }
          ]
        },
        {
          model: dbSAPPS.ClientSubtype,
          as: "clientsubtypes",
          attributes: ["id", "display_name", "code"],
        },
      ],
    });

    return {
      success: true,
      clientTypes,
      message: "Client Types loaded successfully!",
    };
  } catch (error) {
    console.error("Error retrieving Client Types: ", error);
    const errorMessage =
      error.message ||
      "Failed to retrieve Client Types. Please try again later.";
    return {
      success: false,
      message: errorMessage,
    };
  }
}

async function count() {
  try {
    const count = await dbSAPPS.ClientType.count({
      where: { enabled: true },
    });

    return {
      success: true,
      count,
      message: "Client Types loaded successfully!",
    };
  } catch (error) {
    console.error("Error counting Client Types: ", error);
    return {
      success: false,
      message: "Failed to retrieve Client Types count. Please try again later.",
    };
  }
}

async function create(params) {
  try {
    if (!params.company_id)
      return {
        success: false,
        message: "Company is required!",
      };

    if (!params.display_name)
      return {
        success: false,
        message: "Client type is required!",
      };

    const existingClientType = await dbSAPPS.ClientType.findOne({
      where: {
        display_name: params.display_name,
        company_id: params.company_id,
      },
    });

    if (existingClientType)
      return {
        success: false,
        message: `Client Type of "${params.display_name}" is already taken.`,
      };

    if (!params.enabled) params.enabled = true;
    const newClientType = await dbSAPPS.ClientType.create(params);

    return {
      success: true,
      message: "Data Client Type has been successfully adeded!",
      clientTypes: newClientType,
    };
  } catch (error) {
    const errorMessage =
      error.message || "An error occurred during Client type creation.";
    return {
      success: false,
      message: errorMessage,
    };
  }
}

async function update(id, params) {
  try {
    const display_name = await dbSAPPS.ClientType.findByPk(id);
    if (!display_name)
      return {
        success: false,
        message: "Client type not found!",
      };

    if (
      params.display_name &&
      params.display_name !== display_name.display_name &&
      params.company_id &&
      params.company_id !== display_name.company_id
    ) {
      const existingClientTypeName = await dbSAPPS.ClientType.findOne({
        where: {
          display_name: params.display_name,
          company_id: params.company_id,
        },
      });
      if (existingClientTypeName) {
        return {
          success: false,
          message: `Client type of "${params.display_name}" is already taken. Try again!`,
        };
      }
    }

    await display_name.update(params);

    return {
      success: true,
      message: "Your data client type has been saved!",
    };
  } catch (error) {
    const errorMessage = error.message || "Failed to update client type data.";
    return {
      success: false,
      message: errorMessage,
    };
  }
}

async function enabled(id, params) {
  try {
    const clienttype = await dbSAPPS.ClientType.findByPk(id);
    if (!clienttype)
      return {
        success: false,
        message: "Client Type not found!",
      };

    const updateData = {};
    updateData.enabled = params.enabled;
    if (params.modified_by) updateData.modified_by = params.modified_by;
    if (params.modified) updateData.modified = params.modified;

    await clienttype.update(updateData);

    return {
      success: true,
      message: "Client Type has been updated!",
    };
  } catch (error) {
    const errorMessage = error.message || "Failed to Update Client Type.";
    return {
      success: false,
      message: errorMessage,
    };
  }
}

async function _delete(id) {
  try {
    const display_name = await dbSAPPS.ClientType.findByPk(id);
    if (!display_name)
      return { success: false, message: "Client type not found!" };

    await display_name.destroy();

    return {
      success: true,
      message: "Client type successfully deleted!",
    };
  } catch (error) {
    const errorMessage = error.message || "Failed to delete Client type.";
    return {
      success: false,
      message: errorMessage,
    };
  }
}
