import {
  AboutUsPageData,
  CertificatesPageData,
  CharityPageData,
  MainPageData,
} from '@/app/api-utlities/@types/pages';
import {
  presentCertificatesFromTaylor,
  presentCharitiesFromTaylor,
  presentDiscountsFromTaylor,
  presentHistoryItemsFromTaylor,
  presentJobsFromTaylor,
  presentMediaFromTaylor,
  presentPartnersFromTaylor,
  presentReviewsFromTaylor,
  presentStationsFromTaylor,
  presentTeamMembersFromTaylor,
  presentTextsFromTaylor,
} from '@/app/api-utlities/presenters/taylor-presenters';

import { taylorQueryBuilder } from '@/shared/api/taylor-query-builder';

/**
 * Fetches main page content using TaylorDB query builder
 * Replaces the RTK Query GraphQL approach with type-safe query builder
 */
export async function fetchMainPageContent(): Promise<MainPageData> {
  // Use batch queries to fetch all data in a single request
  const [partnersData, jobsData, discountsData, stationsData] =
    await taylorQueryBuilder
      .batch([
        // Fetch partners
        taylorQueryBuilder.selectFrom('partnyory').selectAll(),

        // Fetch jobs
        taylorQueryBuilder.selectFrom('vakansii').selectAll(),

        // Fetch discounts
        taylorQueryBuilder.selectFrom('akcii').selectAll(),

        // Fetch stations
        taylorQueryBuilder.selectFrom('azs').selectAll(),
      ])
      .execute();

  console.log('Loading main page content...');

  return {
    partners: presentPartnersFromTaylor(partnersData),
    jobs: presentJobsFromTaylor(jobsData),
    discounts: presentDiscountsFromTaylor(discountsData),
    stations: presentStationsFromTaylor(stationsData),
  };
}

/**
 * Fetches about us page content using TaylorDB query builder
 */
export async function fetchAboutUsPageContent(): Promise<AboutUsPageData> {
  // Use batch queries to fetch all data in a single request
  const [teamData, historyData, stationsData, reviewsData] =
    await taylorQueryBuilder
      .batch([
        // Fetch team members
        taylorQueryBuilder.selectFrom('komanda').selectAll(),

        // Fetch history items
        taylorQueryBuilder.selectFrom('istoriyaKompanii').selectAll(),

        // Fetch stations
        taylorQueryBuilder
          .selectFrom('azs')
          .selectAll()
          .where('foto', 'isNotEmpty'),

        // Fetch reviews (filtered by published status)
        taylorQueryBuilder
          .selectFrom('otzyvy')
          .selectAll()
          .where('status', '=', 'Опубликовано'),
      ])
      .execute();

  console.log('Loading about us page content...');

  return {
    team: presentTeamMembersFromTaylor(teamData),
    history: presentHistoryItemsFromTaylor(historyData),
    stations: presentStationsFromTaylor(stationsData),
    reviews: presentReviewsFromTaylor(reviewsData),
  };
}

/**
 * Fetches charity page content using TaylorDB query builder
 */
export async function fetchCharityPageContent(): Promise<CharityPageData> {
  const charitiesData = await taylorQueryBuilder
    .selectFrom('blagotvoritelnyjFond')
    .selectAll()
    .execute();

  console.log('Loading charity page content...');

  return {
    charities: presentCharitiesFromTaylor(charitiesData),
  };
}

/**
 * Fetches certificates page content using TaylorDB query builder
 */
export async function fetchCertificatesPageContent(): Promise<CertificatesPageData> {
  const certificatesData = await taylorQueryBuilder
    .selectFrom('sertifikaty')
    .selectAll()
    .execute();

  console.log('Loading certificates page content...');

  return {
    certificates: presentCertificatesFromTaylor(certificatesData),
  };
}

/**
 * Fetches text content using TaylorDB query builder
 */
export async function fetchTextContent(): Promise<
  Array<{ key: string; value: string | null }>
> {
  const textsData = await taylorQueryBuilder
    .selectFrom('tekstovyjKontentSajta')
    .selectAll()
    .execute();

  console.log('Loading text content...');

  return presentTextsFromTaylor(textsData);
}

/**
 * Fetches media content using TaylorDB query builder
 */
export async function fetchMediaContent(): Promise<
  Array<{ key: string; name: string; photo: string | null }>
> {
  const mediaData = await taylorQueryBuilder
    .selectFrom('mediaKontentSajta')
    .selectAll()
    .execute();

  console.log('Loading media content...');

  return presentMediaFromTaylor(mediaData);
}
