import { BadRequestException, Inject, Injectable } from '@nestjs/common';
import { and, count, eq, gte, ilike, inArray, lte, ne, or, SQL, sql } from 'drizzle-orm';
import { NodePgDatabase } from 'drizzle-orm/node-postgres';
import { WINSTON_MODULE_PROVIDER } from 'nest-winston';
import { Logger } from 'winston';
import { ZodError } from 'zod';
import * as schema from '../drizzle/schema.js';
import { businesses, PG_CONNECTION } from '../drizzle/schema.js';
import { FileService } from '../file/file.service.js';
import { GeoService } from '../geo/geo.service.js';
import { BusinessListing, BusinessListingSchema } from '../models/db.model.js';
import { BusinessListingCriteria, emailToDirName, JwtUser } from '../models/main.model.js';
import { convertBusinessToDrizzleBusiness, convertDrizzleBusinessToBusiness, getDistanceQuery } from '../utils.js';

@Injectable()
export class BusinessListingService {
  constructor(
    @Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger,
    @Inject(PG_CONNECTION) private conn: NodePgDatabase<typeof schema>,
    private fileService?: FileService,
    private geoService?: GeoService,
  ) {}

  private getWhereConditions(criteria: BusinessListingCriteria): SQL[] {
    const whereConditions: SQL[] = [];

    if (criteria.city && criteria.searchType === 'exact') {
      whereConditions.push(ilike(businesses.city, `%${criteria.city}%`));
    }
    if (criteria.city && criteria.radius && criteria.searchType === 'radius' && criteria.radius) {
      const cityGeo = this.geoService.getCityWithCoords(criteria.state, criteria.city);
      whereConditions.push(sql`${getDistanceQuery(businesses, cityGeo.latitude, cityGeo.longitude)} <= ${criteria.radius}`);
    }
    if (criteria.types && criteria.types.length > 0) {
      whereConditions.push(inArray(businesses.type, criteria.types));
    }

    if (criteria.state) {
      whereConditions.push(eq(businesses.state, criteria.state));
    }

    if (criteria.minPrice) {
      whereConditions.push(gte(businesses.price, criteria.minPrice));
    }

    if (criteria.maxPrice) {
      whereConditions.push(lte(businesses.price, criteria.maxPrice));
    }

    if (criteria.minRevenue) {
      whereConditions.push(gte(businesses.salesRevenue, criteria.minRevenue));
    }

    if (criteria.maxRevenue) {
      whereConditions.push(lte(businesses.salesRevenue, criteria.maxRevenue));
    }

    if (criteria.minCashFlow) {
      whereConditions.push(gte(businesses.cashFlow, criteria.minCashFlow));
    }

    if (criteria.maxCashFlow) {
      whereConditions.push(lte(businesses.cashFlow, criteria.maxCashFlow));
    }

    if (criteria.minNumberEmployees) {
      whereConditions.push(gte(businesses.employees, criteria.minNumberEmployees));
    }

    if (criteria.maxNumberEmployees) {
      whereConditions.push(lte(businesses.employees, criteria.maxNumberEmployees));
    }

    if (criteria.establishedSince) {
      whereConditions.push(gte(businesses.established, criteria.establishedSince));
    }

    if (criteria.establishedUntil) {
      whereConditions.push(lte(businesses.established, criteria.establishedUntil));
    }

    if (criteria.realEstateChecked) {
      whereConditions.push(eq(businesses.realEstateIncluded, criteria.realEstateChecked));
    }

    if (criteria.leasedLocation) {
      whereConditions.push(eq(businesses.leasedLocation, criteria.leasedLocation));
    }

    if (criteria.franchiseResale) {
      whereConditions.push(eq(businesses.franchiseResale, criteria.franchiseResale));
    }

    if (criteria.title) {
      whereConditions.push(or(ilike(businesses.title, `%${criteria.title}%`), ilike(businesses.description, `%${criteria.title}%`)));
    }

    if (criteria.brokerName) {
      whereConditions.push(or(ilike(schema.users.firstname, `%${criteria.brokerName}%`), ilike(schema.users.lastname, `%${criteria.brokerName}%`)));
    }
    whereConditions.push(and(eq(schema.users.customerType, 'professional'), eq(schema.users.customerSubType, 'broker')));
    return whereConditions;
  }
  async searchBusinessListings(criteria: BusinessListingCriteria, user: JwtUser) {
    const start = criteria.start ? criteria.start : 0;
    const length = criteria.length ? criteria.length : 12;
    const query = this.conn
      .select({
        business: businesses,
        brokerFirstName: schema.users.firstname,
        brokerLastName: schema.users.lastname,
      })
      .from(businesses)
      .leftJoin(schema.users, eq(businesses.email, schema.users.email));

    const whereConditions = this.getWhereConditions(criteria);

    if (whereConditions.length > 0) {
      const whereClause = and(...whereConditions);
      query.where(whereClause);
    }

    // Paginierung
    query.limit(length).offset(start);

    const data = await query;
    const totalCount = await this.getBusinessListingsCount(criteria);
    const results = data.map(r => r.business).map(r => convertDrizzleBusinessToBusiness(r));
    return {
      results,
      totalCount,
    };
  }

  async getBusinessListingsCount(criteria: BusinessListingCriteria): Promise<number> {
    const countQuery = this.conn.select({ value: count() }).from(businesses).leftJoin(schema.users, eq(businesses.email, schema.users.email));

    const whereConditions = this.getWhereConditions(criteria);

    if (whereConditions.length > 0) {
      const whereClause = and(...whereConditions);
      countQuery.where(whereClause);
    }

    const [{ value: totalCount }] = await countQuery;
    return totalCount;
  }

  async findBusinessesById(id: string, user: JwtUser): Promise<BusinessListing> {
    let result = await this.conn
      .select()
      .from(businesses)
      .where(and(sql`${businesses.id} = ${id}`));
    result = result.filter(r => !r.draft || r.imageName === emailToDirName(user?.username) || user?.roles.includes('ADMIN'));
    return convertDrizzleBusinessToBusiness(result[0]) as BusinessListing;
  }

  async findBusinessesByEmail(email: string, user: JwtUser): Promise<BusinessListing[]> {
    const conditions = [];
    conditions.push(eq(businesses.imageName, emailToDirName(email)));
    if (email !== user?.username && (!user?.roles?.includes('ADMIN') ?? false)) {
      conditions.push(ne(businesses.draft, true));
    }
    const listings = (await this.conn
      .select()
      .from(businesses)
      .where(and(...conditions))) as BusinessListing[];

    return listings.map(l => convertDrizzleBusinessToBusiness(l));
  }

  // #### CREATE ########################################
  async createListing(data: BusinessListing): Promise<BusinessListing> {
    try {
      data.created = data.created ? (typeof data.created === 'string' ? new Date(data.created) : data.created) : new Date();
      data.updated = new Date();
      const validatedBusinessListing = BusinessListingSchema.parse(data);
      const convertedBusinessListing = convertBusinessToDrizzleBusiness(data);
      const [createdListing] = await this.conn.insert(businesses).values(convertedBusinessListing).returning();
      return convertDrizzleBusinessToBusiness(createdListing);
    } catch (error) {
      if (error instanceof ZodError) {
        const formattedErrors = error.errors.map(err => ({
          field: err.path.join('.'),
          message: err.message,
        }));
        throw new BadRequestException(formattedErrors);
      }
      throw error;
    }
  }
  // #### UPDATE Business ########################################
  async updateBusinessListing(id: string, data: BusinessListing): Promise<BusinessListing> {
    try {
      data.updated = new Date();
      data.created = data.created ? (typeof data.created === 'string' ? new Date(data.created) : data.created) : new Date();
      const validatedBusinessListing = BusinessListingSchema.parse(data);
      const convertedBusinessListing = convertBusinessToDrizzleBusiness(data);
      const [updateListing] = await this.conn.update(businesses).set(convertedBusinessListing).where(eq(businesses.id, id)).returning();
      return convertDrizzleBusinessToBusiness(updateListing);
    } catch (error) {
      if (error instanceof ZodError) {
        const formattedErrors = error.errors.map(err => ({
          field: err.path.join('.'),
          message: err.message,
        }));
        throw new BadRequestException(formattedErrors);
      }
      throw error;
    }
  }
  // #### DELETE  ########################################
  async deleteListing(id: string): Promise<void> {
    await this.conn.delete(businesses).where(eq(businesses.id, id));
  }
  // ##############################################################
  // States
  // ##############################################################
  async getStates(): Promise<any[]> {
    return await this.conn
      .select({ state: businesses.state, count: sql<number>`count(${businesses.id})`.mapWith(Number) })
      .from(businesses)
      .groupBy(sql`${businesses.state}`)
      .orderBy(sql`count desc`);
  }
}
