import { Injectable } from '@nestjs/common';
import { CreateSignUpNewDto } from './dto/create-sign-up-new.dto';
import { UpdateSignUpNewDto } from './dto/update-sign-up-new.dto';
import { SignUpNew } from './entities/sign-up-new.entity';
import { InjectConnection } from '@nestjs/typeorm';
import { Connection } from 'typeorm';

@Injectable()
export class SignUpNewService {
  constructor(@InjectConnection() private connection: Connection) { }

  create(createSignUpNewDto: SignUpNew) {
    return 'This action adds a new signUpNew';
  }
  async check_old_or_new(userEntity: SignUpNew) {
    const [check_in_users] = await this.connection.query(
      'select id,username from users where users.username = ?', [userEntity.username]
    )
    if (check_in_users) {

      return [{
        "status": "success",
        "userType": "old"
      }];
    } else {
      return [{
        "status": "success",
        "userType": "new"
      }];
    }
  }

  async verifyOld(userEntity: SignUpNew) {
    const [check_password] = await this.connection.query(
      'select id,user_id from users where users.username = ? and users.password = ?', [userEntity.username, userEntity.password]
    )
    if (check_password) {
      console.log(check_password, "------");

      let aa
      if (userEntity.username.length > 10) {
        aa = userEntity.username.slice(2);
      } else {
        aa = userEntity.username
      }
      const getProfile = await this.connection.query(`select
    coalesce(patients.patient_name,"") patient_name,
    coalesce(date(patients.dob),"") dob,
    coalesce(patients.age,"") age,
    coalesce(patients.month,"") month,
    coalesce(patients.day,"") day,
    coalesce(patients.image,"") image,
    coalesce(patients.mobileno,"") mobileno,
    coalesce(patients.email,"") email,
    coalesce(patients.gender,"") gender,
    coalesce(patients.marital_status,"") marital_status,
    coalesce(patients.blood_group,"") blood_group,
    coalesce(concat(coalesce(patients.address,"")," ",coalesce(patients.district_name,"")," ",coalesce(patients.state_name,"")," ",coalesce(patients.pincode,"")),"") address,
    coalesce(patients.guardian_name,"") guardian_name,
    coalesce(patients.patient_type,"") patient_type,
    coalesce(patients.ABHA_number,"") ABHA_number,
    coalesce(patients.known_allergies,"") known_allergies,
    coalesce(patients.insurance_id,"") insurance_id,
    coalesce(patients.insurance_validity,"") insurance_validity,
    coalesce(patients.is_active,"") is_active,
    coalesce(patients.aayush_unique_id,"") aayush_unique_id,
    coalesce(patients.dial_code,"91") dial_code,
    coalesce(patients.salutation,"") salutation,
    coalesce(patients.emergency_dial_code,"") emergency_dial_code,
    coalesce(patients.emergency_mobile_no,"") emergency_mobile_no,
    patients.blood_bank_product_id
    from patients where mobileno = ?`, [aa])
      console.log("getProfile", getProfile);

      if (getProfile.length > 0) {
        return [{
          "messege": "password verified successfully",
          "has_profile":true,
          "user_id":check_password.id,
          "profile_details": getProfile

        }]
      } else {
        return [{
          "messege": "password verified successfully",
          "has_profile":false,
          "user_id":check_password.id,
          "profile_details": getProfile

        }]
      }


    } else {
      return [{
        status: "failed",
        "messege": "password verification failed enter correct password",
      }]
    }

  }

  async createnew(userEntity: SignUpNew) {
    try {
      
        const [check] = await this.connection.query(`select * from users where username = ?`,[userEntity.username])
        console.log(check,"check");
        
      if(check){
        return {
          "status":"failed",
          "message":"user already exist",
          "user_id":check.id,
        }
      }
      const user = await this.connection.query(
        `insert into users (username,password,role) values (?, ?, ?)`, [userEntity.username, userEntity.password, 'patient']
      )
      let aa
      if (userEntity.username.length > 10) {
        aa = userEntity.username.slice(2);
      } else {
        aa = userEntity.username
      }
      console.log(aa, "sssss");

      const getProfile = await this.connection.query(`select
    coalesce(patients.patient_name,"") patient_name,
    coalesce(date(patients.dob),"") dob,
    coalesce(patients.age,"") age,
    coalesce(patients.month,"") month,
    coalesce(patients.day,"") day,
    coalesce(patients.image,"") image,
    coalesce(patients.mobileno,"") mobileno,
    coalesce(patients.email,"") email,
    coalesce(patients.gender,"") gender,
    coalesce(patients.marital_status,"") marital_status,
    coalesce(patients.blood_group,"") blood_group,
    coalesce(concat(coalesce(patients.address,"")," ",coalesce(patients.district_name,"")," ",coalesce(patients.state_name,"")," ",coalesce(patients.pincode,"")),"") address,
    coalesce(patients.guardian_name,"") guardian_name,
    coalesce(patients.patient_type,"") patient_type,
    coalesce(patients.ABHA_number,"") ABHA_number,
    coalesce(patients.known_allergies,"") known_allergies,
    coalesce(patients.insurance_id,"") insurance_id,
    coalesce(patients.insurance_validity,"") insurance_validity,
    coalesce(patients.is_active,"") is_active,
    coalesce(patients.aayush_unique_id,"") aayush_unique_id,
    coalesce(patients.dial_code,"91") dial_code,
    coalesce(patients.salutation,"") salutation,
    coalesce(patients.emergency_dial_code,"") emergency_dial_code,
    coalesce(patients.emergency_mobile_no,"") emergency_mobile_no,
    patients.blood_bank_product_id
    from patients where mobileno = ?`, [aa])
      console.log(getProfile, "getProfile");

      if (getProfile.length > 0) {
        return [{
          "status": "success",
          "messege": "password set successfully",
          "user_id": user.insertId,
          "has_profile":true,
          "user_details": getProfile

        }]
      } else {
        return [{
          "status": "success",
          "messege": "password set successfully",
          "user_id": user.insertId,
          "has_profile":false,
          "user_details": getProfile
        }]
      }


    } catch (error) {
      return error
    }

  }

}
