package repository

import (
	"bpmsapi/dto"
	"bpmsapi/entity"
	"database/sql"
	"errors"
	"fmt"
	"log"
	"strings"

	"github.com/jmoiron/sqlx"
)

type ApartmentActions interface {
	AddApartment(input entity.AddApartmentInput, apartment_img string) ([]entity.ApartmentDetails, error)
	FetchApartmentDetails(input entity.GetApartmentDetailsInput) ([]entity.ApartmentDetails, error)
	UpdateApartmentDetails(input entity.UpdateApartmentInput) (string, error)
	UserApartmentMapping(input entity.UserApartmentMappingInput) ([]entity.UserApartmentMapping, error)
	UpdateUserApartmentInfo(input entity.UserApartmentUpdateInput) ([]entity.UserApartmentMapping, error)
}

type ApartmentRepository struct {
	tx *sqlx.Tx
}

func ApartmentRepositoryInit(tx *sqlx.Tx) *ApartmentRepository {
	return &ApartmentRepository{
		tx: tx,
	}
}

func (apartmentRepo *ApartmentRepository) AddApartment(input entity.AddApartmentInput, apartment_img string) ([]entity.ApartmentDetails, error) {

	query := "INSERT INTO `BPMS_APARTMENT` values()"
	insert, err := apartmentRepo.tx.Exec(query)
	if err != nil {
		apartmentRepo.tx.Rollback()
		log.Println("ApartmentRepository: AddApartment: AddingApartmentid: Error: ", err.Error())
		return nil, err
	}
	last_apartId, inserr := insert.LastInsertId()
	if inserr != nil {
		apartmentRepo.tx.Rollback()
		log.Println("ApartmentRepository: AddApartment: gettingLastInsertId: Error: ", inserr.Error())
		return nil, inserr
	}
	inchargeIns := "INSERT INTO `BPMS_EMP_APARTMENT_MAPPING` (`Emp_Id`, `Apartment_Id`) VALUES (?,?)"
	_, errIn := apartmentRepo.tx.Exec(inchargeIns, input.In_charge, last_apartId)
	if err != nil {
		apartmentRepo.tx.Rollback()
		log.Print("ApartmentRepository: AddApartment: mappingincharge input : Emp_Id:", input.In_charge, " apartment_id:", last_apartId, "Error:", errIn.Error())
		return nil, errIn
	}

	var insQuery string
	var res sql.Result
	var err1 error

	if apartment_img != " " {
		insQuery = "UPDATE `BPMS_APARTMENT` SET `City`=?, `State`=?, `Created_By`=?, `Apartment_Image` = ? WHERE Apartment_Id=?"
		res, err1 = apartmentRepo.tx.Exec(insQuery, input.City_Id, input.State_Id, input.Created_By, apartment_img, last_apartId)
	} else {
		insQuery = "UPDATE `BPMS_APARTMENT` SET `City`=?, `State`=?, `Created_By`=? WHERE Apartment_Id=?"
		res, err1 = apartmentRepo.tx.Exec(insQuery, input.City_Id, input.State_Id, input.Created_By, last_apartId)
	}

	if err1 != nil {
		apartmentRepo.tx.Rollback()
		log.Println("ApartmentRepository: AddApartment: updateCityState: Input:", last_apartId, " Error: ", err1.Error())
		return nil, err1
	}
	rowsAffected, _ := res.RowsAffected()
	if rowsAffected == 0 {
		apartmentRepo.tx.Rollback()
		log.Println("ApartmentRepository: AddApartment: updateCityState :rowsaffected :apartid :", last_apartId)
		return nil, errors.New(dto.ErrorrowsAffectedAddApartment)
	}

	transQuery := "INSERT INTO `BPMS_APARTMENT_TRANSLATIONS`( `Apartment_Id`,`Language_Code`,`Apartment_Name`) values(?,?,?)"
	_, err2 := apartmentRepo.tx.Exec(transQuery, last_apartId, input.Language_Code, input.Apartment_Name)
	if err2 != nil {
		apartmentRepo.tx.Rollback()
		log.Println("ApartmentRepository: AddApartment: Addingapartment: Input:", input.Apartment_Name, "Error: ", err2.Error())
		return nil, err2
	}

	//service apartment mapping
	service_ids := strings.Split(input.Services, ",")
	for _, service_id := range service_ids {
		serviceQuery := "INSERT INTO BPMS_SERVICE_APARTMENT_MAPPING (Apartment_Id, Service_Id) VALUES (?, ?)"
		_, err3 := apartmentRepo.tx.Exec(serviceQuery, last_apartId, service_id)
		if err3 != nil {
			apartmentRepo.tx.Rollback()
			log.Println("ApartmentRepository: AddApartment: mappingserviceid: Input: service_Id:", service_id, " Apartment_Id:", last_apartId, " : error: ", err3.Error())
			return nil, err3
		}
	}

	//response
	var allapartments []entity.ApartmentDetails
	getQuery := apartmentRepo.tx.Select(&allapartments, "SELECT atrans.Apartment_Name, a.Apartment_Id, a.Apartment_Image from BPMS_APARTMENT_TRANSLATIONS atrans INNER JOIN BPMS_APARTMENT a on a.Apartment_Id = atrans.Apartment_Id where atrans.Language_Code = ?", input.Language_Code)
	if getQuery != nil {
		log.Println("ApartmentRepository: AddApartment: fetchingapartment: Error: ", getQuery.Error())
		return nil, getQuery
	} else {
		for i := range allapartments {
			service_details, err1 := apartmentRepo.FetchServiceDetails(allapartments[i].Apartment_Id, input.Language_Code)

			if err1 != nil {
				apartmentRepo.tx.Rollback()
				log.Print("ApartmentRepository: FetchApartmentDetails: FetchServiceDetails : Error:", err1.Error())
				return nil, err1
			}
			allapartments[i].Service_Info = service_details

		}
		return allapartments, nil
	}

}

func (apartmentRepo *ApartmentRepository) FetchApartmentDetails(input entity.GetApartmentDetailsInput) (details []entity.ApartmentDetails, err error) {
	if input.Apartment_Id != 0 {
		err = apartmentRepo.tx.Select(&details, "SELECT ba.Apartment_Id, bat.Apartment_Name, ba.Apartment_Image, bct.City_Name, ba.City, bst.State_Name, ba.State FROM BPMS_APARTMENT ba INNER JOIN BPMS_APARTMENT_TRANSLATIONS bat ON ba.Apartment_Id = bat.Apartment_Id INNER JOIN BPMS_CITY_TRANSLATIONS bct ON ba.City = bct.City_Id AND bct.Language_Code=? INNER JOIN BPMS_STATE_TRANSLATIONS bst ON ba.State = bst.State_Id AND bst.Language_Code=? WHERE bat.Language_Code = ? AND ba.Apartment_Id=?", input.Language_Code, input.Language_Code, input.Language_Code, input.Apartment_Id)
	} else {
		err = apartmentRepo.tx.Select(&details, "SELECT ba.Apartment_Id, bat.Apartment_Name, ba.Apartment_Image, bct.City_Name, ba.City, bst.State_Name, ba.State FROM BPMS_APARTMENT ba INNER JOIN BPMS_APARTMENT_TRANSLATIONS bat ON ba.Apartment_Id = bat.Apartment_Id INNER JOIN BPMS_CITY_TRANSLATIONS bct ON ba.City = bct.City_Id AND bct.Language_Code=? INNER JOIN BPMS_STATE_TRANSLATIONS bst ON ba.State = bst.State_Id AND bst.Language_Code=? WHERE bat.Language_Code = ?", input.Language_Code, input.Language_Code, input.Language_Code)
	}

	if err != nil {
		log.Print("ApartmentRepository: FetchApartmentDetails: gettingallapartmentdetails: Error:", err.Error())
		return nil, err
	} else {
		for i := range details {
			service_details, err1 := apartmentRepo.FetchServiceDetails(details[i].Apartment_Id, input.Language_Code)

			if err1 != nil {
				log.Print("ApartmentRepository: FetchApartmentDetails: FetchServiceDetails : Error:", err1.Error())
				return nil, err1
			}
			details[i].Service_Info = service_details

		}
		for i := range details {
			admin_details, err2 := apartmentRepo.FetchAdminDetails(details[i].Apartment_Id)

			if err2 != nil {
				log.Print("ApartmentRepository: FetchApartmentDetails: FetchAdminDetails : Error:", err2.Error())
				return nil, err2
			}
			details[i].Admin_Info = admin_details

		}
		for i := range details {
			tech_details, errIn := apartmentRepo.FetchTechnicianDetails(details[i].Apartment_Id)

			if errIn != nil {
				log.Print("ApartmentRepository: FetchApartmentDetails: FetchTechnicianDetails : Error:", errIn.Error())
				return nil, errIn
			}
			details[i].Technician_Info = tech_details

		}

		for i := range details {
			basic_info, errInfo := apartmentRepo.FetchApartmentParam(details[i].Apartment_Id)

			if errInfo != nil {
				log.Print("ApartmentRepository: FetchApartmentDetails: FetchApartmentParam : Error:", errInfo.Error())
				return nil, errInfo
			}
			details[i].Basic_Info = basic_info

		}
		for i := range details {
			banner_info, err7 := apartmentRepo.FetchBannerInfo(details[i].Apartment_Id)

			if err7 != nil {
				log.Print("ApartmentRepository: FetchApartmentDetails: FetchApartmentParam : Error:", err7.Error())
				return nil, err7
			}
			details[i].Banner_Info = banner_info

		}
		for i := range details {
			total_user := details[i].Total_User
			err3 := apartmentRepo.tx.Get(&total_user, "SELECT COUNT(User_Id) FROM BPMS_USER WHERE Apartment_Id =?", details[i].Apartment_Id)

			if err3 != nil {
				log.Print("ApartmentRepository: FetchApartmentDetails: total_user : Error:", err3.Error())
				return nil, err3
			}
			details[i].Total_User = total_user
		}
		for i := range details {
			completed_srq := details[i].Completed_Srq
			err4 := apartmentRepo.tx.Get(&completed_srq, "SELECT COUNT(Srq_Id) FROM BPMS_SERVICE_REQUEST WHERE Srq_Apartment_Id = ? AND Srq_Stage_7 = 1 AND Srq_Stage_8 = 0", details[i].Apartment_Id)

			if err4 != nil {
				log.Print("ApartmentRepository: FetchApartmentDetails: completed_service_request : Error:", err4.Error())
				return nil, err4
			}
			details[i].Completed_Srq = completed_srq
		}

		for i := range details {
			pending_srq := details[i].Pending_Srq
			err5 := apartmentRepo.tx.Get(&pending_srq, "SELECT COUNT(Srq_Id) FROM BPMS_SERVICE_REQUEST WHERE Srq_Apartment_Id = ? AND Srq_Stage_7 != 1 AND Srq_Stage_8 != 1", details[i].Apartment_Id)

			if err5 != nil {
				log.Print("ApartmentRepository: FetchApartmentDetails: pending_service_request : Error:", err5.Error())
				return nil, err5
			}
			details[i].Pending_Srq = pending_srq
		}
		for i := range details {
			total_srq := details[i].Total_Srq
			err6 := apartmentRepo.tx.Get(&total_srq, "SELECT COUNT(Srq_Id) FROM BPMS_SERVICE_REQUEST WHERE Srq_Apartment_Id = ? AND Srq_Stage_8 != 1", details[i].Apartment_Id)

			if err6 != nil {
				log.Print("ApartmentRepository: FetchApartmentDetails: total_service_request : Error:", err6.Error())
				return nil, err6
			}
			details[i].Total_Srq = total_srq
		}
		return details, nil
	}

}
func (apartmentRepo *ApartmentRepository) FetchBannerInfo(apartmentid int) (banner []entity.Banner, err error) {
	err = apartmentRepo.tx.Select(&banner, "SELECT Banner_Id, Status, Image_Path FROM BPMS_BANNER WHERE Apartment_Id =?", apartmentid)
	if err != nil {
		log.Print("ApartmentRepository: FetchBannerInfo: input : apartment_id: ", apartmentid, "Error :", err.Error())
		return nil, err
	} else {
		return banner, nil
	}
}
func (apartmentRepo *ApartmentRepository) FetchApartmentParam(apartmentid int) (_ []entity.BasicInfo, err error) {
	apartment_param := []entity.BasicInfo{}
	err = apartmentRepo.tx.Select(&apartment_param, "SELECT  Param_Key, Param_Value, PARAM_SUB FROM BPMS_APARTMENT_PARAM WHERE Apartment_Id = ? AND Validity = 'Yes' ", apartmentid)
	for i := range apartment_param {
		apartmentParam := &apartment_param[i] // Access the current item by reference

		if apartmentParam.Key != nil && *apartmentParam.Key == "Announcement" {
			fmt.Println(apartmentParam.Value)
			paramVal := apartmentParam.Value
			params := strings.Split(paramVal, "$-:")
			if len(params) >= 2 {
				fmt.Println("para", params[0], "am", params[1])
				apartmentParam.Value = params[0]
				apartmentParam.News_Image = &params[1]
			} else if len(params) >= 1 {

				fmt.Println("para", params[0])
				apartmentParam.Value = params[0]

			}
		}
	}
	if err != nil {
		log.Print("ApartmentRepository: FetchApartmentParam: input : apartment_id: ", apartmentid, "Error :", err.Error())
		return nil, err
	} else {
		return apartment_param, nil
	}
}
func (apartmentRepo *ApartmentRepository) FetchServiceDetails(apartmentid int, languagecode int) (service_info []entity.ServiceInfo, err error) {
	err = apartmentRepo.tx.Select(&service_info, "SELECT bsam.Service_Id, bst.Service_Name, bs.Service_Image_Large FROM BPMS_SERVICE_APARTMENT_MAPPING bsam LEFT JOIN BPMS_SERVICE_TRANSLATIONS bst ON bsam.Service_Id = bst.Service_Id LEFT JOIN BPMS_SERVICE bs ON bst.Service_Id = bs.Service_Id WHERE bsam.Apartment_Id =? AND bst.Language_Code = ?", apartmentid, languagecode)
	if err != nil {
		log.Print("ApartmentRepository: FetchServiceDetails: input : ", apartmentid, "Error :", err.Error())
		return nil, err
	} else {
		return service_info, nil
	}
}

func (apartmentRepo *ApartmentRepository) FetchAdminDetails(apartmentid int) (admin_info []entity.AdminInfo, err error) {
	err = apartmentRepo.tx.Select(&admin_info, "SELECT be.Emp_Id, be.Emp_Name, be.Emp_Mobno, be.Alternate_Mobno, be.Emp_Email, be.Emp_Image From BPMS_EMPLOYEE be INNER JOIN BPMS_EMP_APARTMENT_MAPPING beam ON be.Emp_Id = beam.Emp_Id INNER JOIN BPMS_ROLE_MAPPING brm ON beam.Emp_Id = brm.Emp_Id WHERE brm.Role_Id=2 AND beam.Apartment_Id = ?", apartmentid)
	if err != nil {
		log.Print("ApartmentRepository: FetchAdminDetails: input : ", apartmentid, " Error:", err.Error())
		return nil, err
	} else {
		return admin_info, nil
	}
}

func (apartmentRepo *ApartmentRepository) FetchTechnicianDetails(apartmentid int) (tech_info []entity.TechnicianInfo, err error) {
	err = apartmentRepo.tx.Select(&tech_info, "SELECT be.Emp_Id, be.Emp_Name, be.Emp_Mobno, be.Emp_Image From BPMS_EMPLOYEE be INNER JOIN BPMS_EMP_APARTMENT_MAPPING beam ON be.Emp_Id = beam.Emp_Id INNER JOIN BPMS_ROLE_MAPPING brm ON beam.Emp_Id = brm.Emp_Id WHERE brm.Role_Id= 1 AND beam.Apartment_Id = ?", apartmentid)
	if err != nil {
		log.Print("ApartmentRepository: FetchTechnicianDetails: input : ", apartmentid, " Error:", err.Error())
		return nil, err
	} else {
		return tech_info, nil
	}
}
func (apartmentRepo *ApartmentRepository) UpdateApartmentDetails(input entity.UpdateApartmentInput, apartment_img string, banner_imgs []string) (string, error) {

	query := "UPDATE BPMS_APARTMENT ba JOIN BPMS_APARTMENT_TRANSLATIONS bat ON ba.Apartment_Id = bat.Apartment_Id SET ba.Apartment_Image=IFNULL(?, Apartment_Image), bat.Apartment_Name=IFNULL(?, Apartment_Name) WHERE ba.Apartment_Id=? AND bat.Language_Code =?"
	_, err := apartmentRepo.tx.Exec(query, NewNullString(apartment_img), NewNullString(input.Apartment_Name), input.Apartment_Id, input.Language_Code)
	if err != nil {
		apartmentRepo.tx.Rollback()
		log.Print("ApartmentRepository: UpdateApartmentDetails: input : ", input.Apartment_Id, " Error:", err.Error())
		return "Error while updating the apartment details", err
	}
	if strings.ToLower(input.Action) == "i" {

		if len(banner_imgs) > 0 {

			for _, banner_img := range banner_imgs {
				query := "INSERT INTO `BPMS_BANNER` (Status, Image_Path, Apartment_Id) VALUES (?,?,?)"
				_, errIn := apartmentRepo.tx.Exec(query, input.Status, banner_img, input.Apartment_Id)

				if errIn != nil {
					apartmentRepo.tx.Rollback()
					log.Println("ApartmentRepository: UpdateApartmentDetails: insertingbannerimg: Error: ", errIn.Error())
					return "Error while inserting the banner image", err

				}

			}

		}

	} else if strings.ToLower(input.Action) == "d" {

		delQuery := "DELETE FROM `BPMS_BANNER` WHERE Banner_Id=? AND Apartment_Id= ?"
		rowDel, errDel := apartmentRepo.tx.Exec(delQuery, input.Banner_Id, input.Apartment_Id)
		fmt.Println("working")
		if errDel != nil {

			apartmentRepo.tx.Rollback()
			log.Print("ApartmentRepository: UpdateApartmentDetails: deletingbanner: input : Apartment_id:", input.Apartment_Id, " : Banner_Id: ", input.Banner_Id, ": Error:", errDel.Error())
			return "Error while deleting banner image", errDel

		} else {
			rowsAffectedBanner, _ := rowDel.RowsAffected()
			if rowsAffectedBanner == 0 {
				apartmentRepo.tx.Rollback()
				log.Println("ApartmentRepository: UpdateApartmentDetails: deletingbanner: rowsaffected")
				return "Error while deleting banner image ", errors.New(dto.ErrorrowsAffectedUpdateApartmentBanner)
			}
		}

	}

	if input.Services != "" {

		delService := "DELETE FROM `BPMS_SERVICE_APARTMENT_MAPPING` WHERE Apartment_Id = ?"
		rows, err1 := apartmentRepo.tx.Exec(delService, input.Apartment_Id)

		if err1 != nil {
			apartmentRepo.tx.Rollback()
			log.Print("ApartmentRepository: UpdateApartmentDetails: deletingservices: input : Apartment_id:", input.Apartment_Id, "Error:", err1.Error())
			return "Error while unmapping services in apartment", err1

		}

		rowsAffectedSkill, _ := rows.RowsAffected()
		if rowsAffectedSkill == 0 {
			apartmentRepo.tx.Rollback()
			log.Println("ApartmentRepository: UpdateApartmentDetails: serviceapartmentunmapping: rowsaffected")
			return "Error while unmapping services", errors.New(dto.ErrorrowsAffectedUpdateApartment)
		}

		service_ids := strings.Split(input.Services, ",")
		for _, service_id := range service_ids {
			serviceQuery := "INSERT INTO BPMS_SERVICE_APARTMENT_MAPPING (Apartment_Id, Service_Id) VALUES (?,?)"
			_, err2 := apartmentRepo.tx.Exec(serviceQuery, input.Apartment_Id, service_id)
			if err2 != nil {
				apartmentRepo.tx.Rollback()
				log.Println("ApartmentRepository: UpdateApartmentDetails: serviceapartmentmapping: Input: service_Id:", service_id, " Apartment_Id:", input.Apartment_Id, " Error: ", err2.Error())
				return "Error while updating services", err2
			}
		}

	}
	return "Apartment details updated successfully", nil
}

func (apartmentRepo *ApartmentRepository) UserApartmentMapping(input entity.UserApartmentMappingInput) (userapartment_info []entity.UserApartmentMapping, err error) {
	insQuery := "INSERT INTO `BPMS_USER_APARTMENT_MAPPING` (User_Id,  User_Type, Apartment_Id, Door_No, Block, GSTN, TIN, PAN, Billing_Address_Id)  values(?, ?, ?, ?, ?, ?, ?, ?,?)"
	_, err = apartmentRepo.tx.Exec(insQuery, input.User_Id, input.User_Type, input.Apartment_Id, input.Doorno, input.Block, input.GSTN, input.TIN, input.PAN, input.Billing_Address_Id)
	if err != nil {
		apartmentRepo.tx.Rollback()
		log.Println("ApartmentRepository: UserApartmentMapping: MappingNewApartment: userid: ,", input.User_Id, "Error: ", err.Error())
		return nil, err
	} else {

		errIn := apartmentRepo.tx.Select(&userapartment_info, "SELECT buam.User_Id, bu.User_Name, bu.User_Image, bu.User_Email, bu.User_Mobno, bu.Alternate_No, bu.Apartment_Purchase_Date, buam.Apartment_Id, bat.Apartment_Name,ba.Apartment_Image, buam.User_Type, buam.Door_No, buam.Block, bct.City_Name, bst.State_Name, buam.GSTN, buam.TIN, buam.PAN, buam.Billing_Address_Id, bas.Country as Billing_Country, bas.State as Billing_State, bas.City as Billing_City, bas.Area_Name as Billing_Area_Name, bas.Address_Line1 as Billing_Address_Line1, bas.Address_Line2 as Billing_Address_Line2, bas.Pincode as Billing_Pincode, bas.Latitude as Billing_Latitude, bas.Longitude as Billing_Longitude FROM BPMS_USER_APARTMENT_MAPPING buam INNER JOIN BPMS_USER bu ON buam.User_Id = bu.User_Id INNER JOIN BPMS_APARTMENT ba ON buam.Apartment_Id = ba.Apartment_Id INNER JOIN BPMS_STATE_TRANSLATIONS bst ON ba.State = bst.State_Id AND bst.Language_Code = 1 INNER JOIN BPMS_CITY_TRANSLATIONS bct ON ba.City  = bct.City_Id AND bct.Language_Code =1 INNER JOIN BPMS_APARTMENT_TRANSLATIONS bat ON buam.Apartment_Id = bat.Apartment_Id AND bat.Language_Code =1 LEFT JOIN BPMS_ADDRESS bas ON buam.Billing_Address_Id = bas.Address_Id WHERE buam.User_Id = ? AND buam.Apartment_Id =?", input.User_Id, input.Apartment_Id)
		if errIn != nil {
			log.Print("ApartmentRepository: UserApartmentMapping:  mappinguserapartment :apartmentid : ", input.Apartment_Id, " : userid:", input.User_Id, "Error:", errIn.Error())
			return nil, errIn
		} else {
			log.Print("ApartmentRepository: UserApartmentMapping:  mappinguserapartment :apartmentid : ", input.Apartment_Id, " : userid:", input.User_Id)
			return userapartment_info, nil
		}
	}

}

func (apartmentRepo *ApartmentRepository) UpdateUserApartmentInfo(input entity.UserApartmentUpdateInput) (userapartment_info []entity.UserApartmentMapping, err error) {
	updateQuery := "UPDATE BPMS_USER_APARTMENT_MAPPING SET User_Type = IFNULL(?, User_Type), Door_No = IFNULL(?, Door_No), Block=IFNULL(?, Block), GSTN=IFNULL(?, GSTN), TIN=IFNULL(?, TIN), PAN=IFNULL(?, PAN), Billing_Address_Id=IFNULL(?, Billing_Address_Id) WHERE  User_Id= ? AND Apartment_Id= ?"

	_, err = apartmentRepo.tx.Exec(updateQuery, NewNullInt64(int64(input.User_Type)), NewNullString(input.Doorno), NewNullString(input.Block), NewNullString(input.GSTN), NewNullString(input.TIN), NewNullString(input.PAN), NewNullInt64(int64(input.Billing_Address_Id)), input.User_Id, input.Apartment_Id)
	if err != nil {
		apartmentRepo.tx.Rollback()
		log.Println("ApartmentRepository: UpdateUserApartmentInfo: updatingapartmentdetails: userid: ,", input.User_Id, "Error: ", err.Error())
		return nil, err
	} else {

		errIn := apartmentRepo.tx.Select(&userapartment_info, "SELECT buam.User_Id, bu.User_Name, bu.User_Image, bu.User_Email, bu.User_Mobno, bu.Alternate_No, bu.Apartment_Purchase_Date, buam.Apartment_Id, bat.Apartment_Name,ba.Apartment_Image, buam.User_Type, buam.Door_No, buam.Block, bct.City_Name, bst.State_Name, buam.GSTN, buam.TIN, buam.PAN, buam.Billing_Address_Id, bas.Country as Billing_Country, bas.State as Billing_State, bas.City as Billing_City, bas.Area_Name as Billing_Area_Name, bas.Address_Line1 as Billing_Address_Line1, bas.Address_Line2 as Billing_Address_Line2, bas.Pincode as Billing_Pincode, bas.Latitude as Billing_Latitude, bas.Longitude as Billing_Longitude FROM BPMS_USER_APARTMENT_MAPPING buam INNER JOIN BPMS_USER bu ON buam.User_Id = bu.User_Id INNER JOIN BPMS_APARTMENT ba ON buam.Apartment_Id = ba.Apartment_Id INNER JOIN BPMS_STATE_TRANSLATIONS bst ON ba.State = bst.State_Id AND bst.Language_Code = 1 INNER JOIN BPMS_CITY_TRANSLATIONS bct ON ba.City  = bct.City_Id AND bct.Language_Code =1 INNER JOIN BPMS_APARTMENT_TRANSLATIONS bat ON buam.Apartment_Id = bat.Apartment_Id AND bat.Language_Code =1 LEFT JOIN BPMS_ADDRESS bas ON buam.Billing_Address_Id = bas.Address_Id WHERE buam.User_Id = ? AND buam.Apartment_Id =?", input.User_Id, input.Apartment_Id)
		if errIn != nil {
			log.Print("ApartmentRepository: UpdateUserApartmentInfo:  gettingapartmentdetails :apartmentid : ", input.Apartment_Id, " : userid:", input.User_Id, "Error:", errIn.Error())
			return nil, errIn
		} else {
			log.Print("ApartmentRepository: UpdateUserApartmentInfo:  gettingapartmentdetails :apartmentid : ", input.Apartment_Id, " : userid:", input.User_Id)
			return userapartment_info, nil
		}
	}

}
