const sequelize = require("../config/dbconnection")
const Sequelize = require('sequelize')


const CustomerType = sequelize.define('customer_type', {
    id: { type: Sequelize.INTEGER, allowNull: false, primaryKey: true, autoIncrement: true },
    name: { type: Sequelize.STRING, allowNull: false },
    billing_type: { type: Sequelize.FLOAT(12, 2), allowNull: false},
    payment_days_only: { type: Sequelize.INTEGER, allowNull: true },
    payment_type: { type: Sequelize.STRING, allowNull: true },
    payment_days: { type: Sequelize.INTEGER, allowNull: true },
    payment_months: { type: Sequelize.INTEGER, allowNull: true },
    quickbookId: { type: Sequelize.INTEGER, allowNull: true,defaultValue:null},
    status: { type: Sequelize.INTEGER, allowNull: false,defaultValue:1},
}, { timestamps: false, tableName: 'customer_type' })


const addCustomerType = async (customerTypeData) => {
    try {
      const newCustomerType = await CustomerType.create(customerTypeData);
      return newCustomerType;
    } catch (error) {
      console.error('Error adding customer type:', error);
      throw error;
    }
  };

  

  const editCustomerType = async (id, updatedData) => {
    try {
      const result = await CustomerType.update(updatedData, {
        where: { id },
      });
  
      if (result[0] === 1) {
        return { message: 'Customer type updated successfully' };
      } else {
        return { message: 'Customer type not found or no changes made' };
      }
    } catch (error) {
      console.error('Error editing customer type:', error);
      throw error;
    }
  };

  
  const findCustomerTypeById = async (id) => {
    try {
        const customerType = await CustomerType.findOne({
            where: {
              id,
              status: 1, 
            },
          });
      if (customerType) {
        return customerType;
      } else {
        return { message: 'Customer type not found' };
      }
    } catch (error) {
      console.error('Error finding customer type:', error);
      throw error;
    }
  };

  const findAllCustomerType= async () => {
    try {
        const customerTypes = await CustomerType.findAll({
            where: {
              status: 1, // Add condition to check if status is 1
            },
          })
      if (customerTypes) {
        return customerTypes;
      } else {
        return { message: 'Customer type not found' };
      }
    } catch (error) {
      console.error('Error finding customer type:', error);
      throw error;
    }
  };

  

  const deleteCustomerType = async (id,status) => {
    try {
      const result = await CustomerType.update(
        { status: status }, // Set status to 0 instead of deleting
        {
          where: { id },
        }
      );
  
      if (result[0] === 1) {
        return { message: 'Customer type status updated to 0 (soft deleted) successfully' };
      } else {
        return { message: 'Customer type not found' };
      }
    } catch (error) {
      console.error('Error updating customer type status:', error);
      throw error;
    }
  };
  
  


  
module.exports = {
    addCustomerType,
    editCustomerType,
    findCustomerTypeById,
    deleteCustomerType,
    findAllCustomerType
 }