import { sql } from 'drizzle-orm';
import { businesses, commercials, users } from './drizzle/schema.js';
import { User } from './models/db.model.js';
export const EARTH_RADIUS_KM = 6371; // Erdradius in Kilometern
export const EARTH_RADIUS_MILES = 3959; // Erdradius in Meilen
export function convertStringToNullUndefined(value) {
  // Konvertiert den Wert zu Kleinbuchstaben für eine case-insensitive Überprüfung
  const lowerCaseValue = typeof value === 'boolean' ? value : value?.toLowerCase();

  if (lowerCaseValue === 'null') {
    return null;
  } else if (lowerCaseValue === 'undefined') {
    return undefined;
  }

  // Gibt den Originalwert zurück, wenn es sich nicht um 'null' oder 'undefined' handelt
  return value;
}

export const getDistanceQuery = (schema: typeof businesses | typeof commercials | typeof users, lat: number, lon: number, unit: 'km' | 'miles' = 'miles') => {
  const radius = unit === 'km' ? EARTH_RADIUS_KM : EARTH_RADIUS_MILES;

  return sql`
    ${radius} * 2 * ASIN(SQRT(
      POWER(SIN((${lat} - ${schema.latitude}) * PI() / 180 / 2), 2) +
      COS(${lat} * PI() / 180) * COS(${schema.latitude} * PI() / 180) *
      POWER(SIN((${lon} - ${schema.longitude}) * PI() / 180 / 2), 2)
    ))
  `;
};

type DrizzleUser = typeof users.$inferSelect; //Partial<InferInsertModel<typeof users>>;

export function convertUserToDrizzleUser(user: Partial<User>): DrizzleUser {
  const { companyLocation, ...restUser } = user;

  // Ensure all required fields are present
  if (!user.email || !user.firstname || !user.lastname) {
    throw new Error('Missing required fields: email, firstname, or lastname');
  }

  return {
    id: user.id,
    email: user.email,
    firstname: user.firstname,
    lastname: user.lastname,
    phoneNumber: user.phoneNumber || null,
    description: user.description || null,
    companyName: user.companyName || null,
    companyOverview: user.companyOverview || null,
    companyWebsite: user.companyWebsite || null,
    city: companyLocation?.city || null,
    state: companyLocation?.state || null,
    offeredServices: user.offeredServices || null,
    areasServed: user.areasServed || [],
    hasProfile: user.hasProfile || false,
    hasCompanyLogo: user.hasCompanyLogo || false,
    licensedIn: user.licensedIn || [],
    gender: user.gender || null,
    customerType: user.customerType || null,
    customerSubType: user.customerSubType || null,
    created: user.created || new Date(),
    updated: user.updated || new Date(),
    latitude: companyLocation?.latitude || 0,
    longitude: companyLocation?.longitude || 0,
  };
}

export function convertDrizzleUserToUser(drizzleUser: Partial<DrizzleUser>): User {
  const user = {
    id: drizzleUser.id,
    firstname: drizzleUser.firstname,
    lastname: drizzleUser.lastname,
    email: drizzleUser.email,
    phoneNumber: drizzleUser.phoneNumber ?? null,
    description: drizzleUser.description ?? null,
    companyName: drizzleUser.companyName ?? null,
    companyOverview: drizzleUser.companyOverview ?? null,
    companyWebsite: drizzleUser.companyWebsite ?? null,
    companyLocation: {
      city: drizzleUser.city,
      state: drizzleUser.state,
      latitude: drizzleUser.latitude, // Latitude wird zugewiesen, auch wenn es nicht direkt benötigt wird
      longitude: drizzleUser.longitude, // Longitude wird zugewiesen, auch wenn es nicht direkt benötigt wird
    },
    offeredServices: drizzleUser.offeredServices ?? null,
    areasServed: drizzleUser.areasServed ?? null,
    hasProfile: drizzleUser.hasProfile ?? null,
    hasCompanyLogo: drizzleUser.hasCompanyLogo ?? null,
    licensedIn: drizzleUser.licensedIn ?? null,
    gender: drizzleUser.gender ?? null,
    customerType: drizzleUser.customerType,
    customerSubType: drizzleUser.customerSubType ?? null,
    created: drizzleUser.created ?? null,
    updated: drizzleUser.updated ?? null,
  };
  return user;
}
