import { Injectable, OnModuleInit } from "@nestjs/common";
import { InjectDataSource } from "@nestjs/typeorm";
import { DataSource } from "typeorm";

@Injectable()
export class AppService implements OnModuleInit {
  constructor(private readonly connection: DataSource) { }
  async onModuleInit() {
    try {
      await this.connection.query(`
        CREATE TABLE IF NOT EXISTS otp(
          id BIGINT AUTO_INCREMENT PRIMARY KEY,
          mobileno varchar(20),
         generated_otp INT,
         request_id varchar(50),
         resend_otp_count INT,
          created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
        )
      `);
    } catch (error) {
      console.error("Failed to create otp table :", error.message);
    }
    try {
      await this.connection.query(`ALTER TABLE user_address 
DROP COLUMN address,
ADD COLUMN house_no VARCHAR(145) NOT NULL AFTER longitude,
ADD COLUMN area_name TEXT NOT NULL AFTER house_no,
ADD COLUMN nearest_landmark TEXT NULL AFTER area_name,
ADD COLUMN pincode VARCHAR(45) NOT NULL AFTER nearest_landmark,
ADD COLUMN address_type VARCHAR(45) NOT NULL AFTER pincode;
`)
    } catch (error) {

    }
  }

  getHello(): string {
    return "Hello PHR!";
  }
}
