import { TaylorDatabase } from '@/shared/types/database.types';

type TaylorColumnRaw<TColumn> = TColumn extends {
  raw: infer TRaw;
  isRequired: true;
}
  ? TRaw
  : TColumn extends { raw: infer TRaw }
    ? TRaw | undefined
    : never;

type TableRaws<TTableName extends keyof TaylorDatabase> = {
  [K in keyof TaylorDatabase[TTableName]]: TaylorColumnRaw<
    TaylorDatabase[TTableName][K]
  >;
};

const getAttachmentUrl = (urls: string[] | undefined): string | null => {
  if (!urls?.length) return null;

  const [url] = urls;
  if (!url) return null;

  if (/^https?:\/\//.test(url)) {
    return url;
  }

  return process.env.TAYLOR_MEDIA_URL
    ? `${process.env.TAYLOR_MEDIA_URL}/${url}`
    : url;
};

// Presenters for TaylorDB query builder results (direct array format, no wrapper)

export const presentPartnersFromTaylor = (
  partners: TableRaws<'partnyory'>[],
): Array<{ id: number; name: string; poster: string | null }> => {
  return partners.map((partner, index) => ({
    id: index + 1,
    name: partner.nazvanie || '',
    poster: getAttachmentUrl(partner.izobrozhenie),
  }));
};

export const presentJobsFromTaylor = (
  jobs: TableRaws<'vakansii'>[],
): Array<{
  id: number;
  name: string;
  tags: string[];
  location: string | null;
  type: string | null;
}> => {
  return jobs.map((job, index) => ({
    id: index + 1,
    name: job.zagolovok || '',
    tags: job.tegi || [],
    location: job.lokaciya || null,
    type: job.tip || null,
  }));
};

export const presentDiscountsFromTaylor = (
  discounts: TableRaws<'akcii'>[],
): Array<{
  id: number;
  name: string;
  description: string;
  expiresAt: string;
  image: string | null;
}> => {
  return discounts.map((discount, index) => ({
    id: index + 1,
    name: discount.zagolovok || '',
    description: discount.opisanie || '',
    expiresAt: discount.do || '',
    image: getAttachmentUrl(discount.foto),
  }));
};

export const presentStationsFromTaylor = (
  stations: TableRaws<'azs'>[],
): Array<{
  id: number;
  name: string;
  description: string;
  address: string;
  workingHours: string | null;
  latitude: number;
  longitude: number;
  carWash: boolean;
  ai92: boolean;
  ai95: boolean;
  dt: boolean;
  z100: boolean;
  propan: boolean;
  electricCharge: boolean;
  miniMarket: boolean;
  toilet: boolean;
  region: string | null;
  image: string | null;
}> => {
  return stations.map((station, index) => ({
    id: index + 1,
    name: station.imya || '',
    description: station.opisanie || '',
    address: station.adress || '',
    workingHours: station.chasyRaboty || null,
    latitude: parseFloat(station.lat || '0') || 0,
    longitude: parseFloat(station.long || '0') || 0,
    carWash: station.avtomojka || false,
    ai92: station.ai92 || false,
    ai95: station.ai95 || false,
    dt: station.dt || false,
    z100: station.z100 || false,
    propan: station.propan || false,
    electricCharge: station.zaryadnayaStanciya || false,
    miniMarket: station.miniMarket || false,
    toilet: station.tualet || false,
    region: station.region || null,
    image: getAttachmentUrl(station.foto),
  }));
};

export const presentTeamMembersFromTaylor = (
  members: TableRaws<'komanda'>[],
): Array<{
  name: string;
  photo: string | null;
  profession: string;
}> => {
  return members.map((member) => ({
    name: member.polnoeImya || '',
    photo: getAttachmentUrl(member.foto),
    profession: member.zvanie || '',
  }));
};

export const presentHistoryItemsFromTaylor = (
  historyItems: TableRaws<'istoriyaKompanii'>[],
): Array<{
  name: string;
  year: string;
  description: string;
}> => {
  return historyItems.map((item) => ({
    name: item.zagolovok || '',
    year: String(item.god || ''),
    description: item.opisanie || '',
  }));
};

export const presentReviewsFromTaylor = (
  reviews: TableRaws<'otzyvy'>[],
): Array<{
  id: number;
  fullname: string;
  review: string;
  rating: number;
}> => {
  return reviews.map((review) => ({
    id: review.id || 0,
    fullname: review.polnoeImya || '',
    review: review.otzyv || '',
    rating: review.rejting || 0,
  }));
};

export const presentCharitiesFromTaylor = (
  charities: TableRaws<'blagotvoritelnyjFond'>[],
): Array<{
  id: number;
  name: string;
  description: string;
  date: string;
  location: string;
  image: string | null;
}> => {
  return charities.map((charity, index) => ({
    id: index + 1,
    name: charity.zagolovok || '',
    description: charity.opisanie || '',
    date: charity.data || '',
    location: charity.lokaciya || '',
    image: getAttachmentUrl(charity.foto),
  }));
};

export const presentCertificatesFromTaylor = (
  certificates: TableRaws<'sertifikaty'>[],
): Array<{
  id: number;
  name: string;
  description: string;
  issuedAt: string;
  validUntil: string;
  image: string | null;
}> => {
  return certificates.map((certificate, index) => ({
    id: index + 1,
    name: certificate.nazvanie || '',
    description: certificate.opisanie || '',
    issuedAt: certificate.dataVydachi || '',
    validUntil: certificate.dejstvitelenDo || '',
    image: getAttachmentUrl(certificate.foto),
  }));
};

export const presentTextsFromTaylor = (
  texts: TableRaws<'tekstovyjKontentSajta'>[],
): Array<{
  key: string;
  value: string | null;
}> => {
  return texts.map((item) => ({
    key: item.klyuchNeIzmenyat || '',
    value: item.znachenie || null,
  }));
};

export const presentMediaFromTaylor = (
  media: TableRaws<'mediaKontentSajta'>[],
): Array<{
  key: string;
  name: string;
  photo: string | null;
}> => {
  return media.map((record) => ({
    key: record.klyuchNeIzmenyat || '',
    name: record.mestopolozheniya || '',
    photo: getAttachmentUrl(record.foto),
  }));
};
