// app/page.tsx
import Flex from '@/components/Blocks/Flex/Flex';
import Grid from '@/components/Blocks/Grid/Grid';
import IFrame from '@/components/Blocks/IFrame/IFrame';
import Section from '@/components/Blocks/Section/Section';
import PostCard from '@/components/Post/PostCard';
import EmblaCarousel from '@/components/UI/EmblaCarousel/EmblaCarousel';
import Stat from '@/components/UI/Stat/Stat';
import Areas from '@/components/WP/Areas/Areas';
import { SmartSlider } from '@/components/WPRenderer/SmartSlider';
import { renderPostContent } from '@/components/WPRenderer/WPRenderer';
import { connection } from '@/lib/duckdb';
import { transformLinks } from '@/lib/utils';
import { CarouselSlide, PostData } from '@/types/entities';

export const revalidate = 10;

export default async function HomePage() {
    const duckRes = await connection.run(`
            SELECT p.* 
            FROM wp_posts p
            WHERE p.ID = (
                SELECT option_value 
                FROM wp_options 
                WHERE option_name = 'page_on_front'
                LIMIT 1
            )
            LIMIT 1;`);

    const duckData = await duckRes.getRowObjectsJson()

    const baseUrl = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3000';

    const res = await fetch(`${baseUrl}/api/home`, { next: { revalidate: 10 }, });

    if (!res.ok) {
        // 🚨 REQUIRED for stale reuse
        throw new Error('Failed to fetch home posts');
    }

    //const posts: PostData[] = await res.json();
    const posts = duckData

    const stats: { title: string, value: string }[] = [
        {
            title: 'территории Якутии',
            value: '74 %'
        },
        {
            title: 'муниципальных районов',
            value: '26'
        },
        {
            title: 'теплоснабжающих объектов',
            value: '637'
        },
        {
            title: 'Гкал/ч установленная мощность',
            value: '2645,4'
        },
        {
            title: 'км. инженерных сетей',
            value: '2818,5'
        },
        {
            title: 'работников',
            value: '8446'
        }
    ]

    const areaSlides: CarouselSlide[] = [
        {
            id: 0,
            title: 'Абыйский',
            src: 'https://new.jkhsakha.ru/wp-content/uploads/elementor/thumbs/%D0%90%D0%B1%D1%8B%D0%B9%D1%81%D0%BA%D0%B8%D0%B9-pxq84cwhvn5ot9eugai2c8bxn40u3rd9vx6c1fzro0.jpg'
        },
        {
            id: 1,
            title: 'Аллаиховский',
            src: 'https://new.jkhsakha.ru/wp-content/uploads/elementor/thumbs/%D0%90%D0%BB%D0%BB%D0%B0%D0%B8%D1%85%D0%BE%D0%B2%D1%81%D0%BA%D0%B8%D0%B9-pxq84gnumzau3p9duc4km7ds0niayjs78fs9yju6z4.jpg'
        },
        {
            id: 3,
            title: 'Амгинский',
            src: 'https://new.jkhsakha.ru/wp-content/uploads/elementor/thumbs/%D0%90%D0%BC%D0%B3%D0%B8%D0%BD%D1%81%D0%BA%D0%B8%D0%B9-pxq84kf7ebfze53x8dr2w6fme6zrtc74kye7vnoma8.jpg'
        },
        {
            id: 4,
            title: 'Анабарский',
            src: 'https://new.jkhsakha.ru/wp-content/uploads/elementor/thumbs/%D0%90%D0%BD%D0%B0%D0%B1%D0%B0%D1%80%D1%81%D0%BA%D0%B8%D0%B9-pxq84o6k5nl4okygmfdl65hgrqh8o4m1xh05srj1lc.jpg'
        },
        {
            id: 5,
            title: 'Булунский',
            src: 'https://new.jkhsakha.ru/wp-content/uploads/elementor/thumbs/%D0%91%D1%83%D0%BB%D1%83%D0%BD%D1%81%D0%BA%D0%B8%D0%B9-pxq84rxwwzq9z0t00h03g4jb59ypix0z9zm3pvdgwg.jpg'
        },
        {
            id: 6,
            title: 'Верхневилюйский',
            src: 'https://new.jkhsakha.ru/wp-content/uploads/elementor/thumbs/%D0%92%D0%B5%D1%80%D1%85%D0%BD%D0%B5%D0%B2%D0%B8%D0%BB%D1%8E%D0%B9%D1%81%D0%BA%D0%B8%D0%B9-pxq84vp9obvf9gnjeimlq3l5itg6dpfwmi81mz7w7k.jpg'
        },
        {
            id: 7,
            title: 'Верхнеколымский',
            src: 'https://new.jkhsakha.ru/wp-content/uploads/elementor/thumbs/%D0%92%D0%B5%D1%80%D1%85%D0%BD%D0%B5%D0%BA%D0%BE%D0%BB%D1%8B%D0%BC%D1%81%D0%BA%D0%B8%D0%B9-pxq84zgmfo0kjwi2sk9402mzwcxn8hutz0tzk32bio.jpg'
        },
        {
            id: 8,
            title: 'Вилюйский',
            src: 'https://new.jkhsakha.ru/wp-content/uploads/elementor/thumbs/%D0%92%D0%B8%D0%BB%D1%8E%D0%B9%D1%81%D0%BA%D0%B8%D0%B9-pxq857x656c5ge5sf5wr4ii58try5rsf06pcvkpryo.jpg'
        },
        {
            id: 9,
            title: 'Верхоянский',
            src: 'https://new.jkhsakha.ru/wp-content/uploads/elementor/thumbs/%D0%92%D0%B5%D1%80%D1%85%D0%BE%D1%8F%D0%BD%D1%81%D0%BA%D0%B8%D0%B9-pxq8545tdu705yb914a8ujgavaahazdhno3eygvcnk.jpg'
        },
        {
            id: 10,
            title: 'Горный',
            src: 'https://new.jkhsakha.ru/wp-content/uploads/elementor/thumbs/%D0%93%D0%BE%D1%80%D0%BD%D1%8B%D0%B9-pxq85cmd3cil2fyynpxvyzbg7r4s89b2otys9yit3k.jpg'
        },
        {
            id: 11,
            title: 'Жиганский',
            src: 'https://new.jkhsakha.ru/wp-content/uploads/elementor/thumbs/%D0%96%D0%B8%D0%B3%D0%B0%D0%BD%D1%81%D0%BA%D0%B8%D0%B9-pxq85gdpuonqcvti1rke8ydalam931q01ckq72d8eo.jpg'
        },
        {
            id: 12,
            title: 'Заречный Кобяй',
            src: 'https://new.jkhsakha.ru/wp-content/uploads/elementor/thumbs/%D0%97%D0%B0%D1%80%D0%B5%D1%87%D0%BD%D1%8B%D0%B9-%D0%9A%D0%BE%D0%B1%D1%8F%D0%B9-pxq85k52m0svnbo1ft6wixf4yu3pxu4xdv6o467nps.jpg'
        },
        {
            id: 13,
            title: 'Кобяйский',
            src: 'https://new.jkhsakha.ru/wp-content/uploads/elementor/thumbs/%D0%9A%D0%BE%D0%B1%D1%8F%D0%B9%D1%81%D0%BA%D0%B8%D0%B9-pxq85nwfdcy0xriktuteswgzcdl6smjuqdsm1a230w.jpg'
        },
        {
            id: 14,
            title: 'Мегино-Кангаласский',
            src: 'https://new.jkhsakha.ru/wp-content/uploads/elementor/thumbs/%D0%9C%D0%B5%D0%B3%D0%B8%D0%BD%D0%BE-%D0%9A%D0%B0%D0%BD%D0%B3%D0%B0%D0%BB%D0%B0%D1%81%D1%81%D0%BA%D0%B8%D0%B9-pxq85rns4p3687d47wfx2vitpx2nneys2wejydwic0.jpg'
        },
        {
            id: 15,
            title: 'Момский',
            src: 'https://new.jkhsakha.ru/wp-content/uploads/elementor/thumbs/%D0%9C%D0%BE%D0%BC%D1%81%D0%BA%D0%B8%D0%B9-pxq85vf4w18bin7nly2fcuko3gk4i7dpff0hvhqxn4.jpg'
        },
        {
            id: 16,
            title: 'Нижнеколымский',
            src: 'https://new.jkhsakha.ru/wp-content/uploads/elementor/thumbs/%D0%9D%D0%B8%D0%B6%D0%BD%D0%B5%D0%BA%D0%BE%D0%BB%D1%8B%D0%BC%D1%81%D0%BA%D0%B8%D0%B9-pxq8604bu7er4p0tui3k7bdz2dwykowd429x9vjys0.jpg'
        },
        {
            id: 17,
            title: 'Нюрбинский',
            src: 'https://new.jkhsakha.ru/wp-content/uploads/elementor/thumbs/%D0%9D%D1%8E%D1%80%D0%B1%D0%B8%D0%BD%D1%81%D0%BA%D0%B8%D0%B9-pxq863voljjwf4vd8jq2haftfxeffhbagkvv6zee34.jpg'
        },
        {
            id: 18,
            title: 'Олекминский',
            src: 'https://new.jkhsakha.ru/wp-content/uploads/elementor/thumbs/%D0%9E%D0%BB%D0%B5%D0%BA%D0%BC%D0%B8%D0%BD%D1%81%D0%BA%D0%B8%D0%B9-pxq867n1cvp1pkpwmlckr9hntgvwa9q7t3ht438te8.jpg'
        },
        {
            id: 19,
            title: 'Оленекский',
            src: 'https://new.jkhsakha.ru/wp-content/uploads/elementor/thumbs/%D0%9E%D0%BB%D0%B5%D0%BD%D0%B5%D0%BA%D1%81%D0%BA%D0%B8%D0%B9-pxq86bee47u700kg0mz318ji70dd52555m3r1738pc.jpg'
        },
        {
            id: 20,
            title: 'Среднеколымский',
            src: 'https://new.jkhsakha.ru/wp-content/uploads/elementor/thumbs/%D0%A1%D1%80%D0%B5%D0%B4%D0%BD%D0%B5%D0%BA%D0%BE%D0%BB%D1%8B%D0%BC%D1%81%D0%BA%D0%B8%D0%B9-pxq86f5qvjzcagezeollb7lckjutzuk2i4poyaxo0g.jpg'
        },
        {
            id: 21,
            title: 'Сунтарский',
            src: 'https://new.jkhsakha.ru/wp-content/uploads/elementor/thumbs/%D0%A1%D1%83%D0%BD%D1%82%D0%B0%D1%80%D1%81%D0%BA%D0%B8%D0%B9-pxq86juxtq5rwi85n8mq5oenjh7o2c2q6rz4coqp5c.jpg'
        },
        {
            id: 22,
            title: 'Таттинский',
            src: 'https://new.jkhsakha.ru/wp-content/uploads/elementor/thumbs/%D0%A2%D0%B0%D1%82%D1%82%D0%B8%D0%BD%D1%81%D0%BA%D0%B8%D0%B9-pxq86nmal2ax6y2p1a98fnghx0p4x4hnjal29sl4gg.jpg'
        },
        {
            id: 23,
            title: 'Томпонский',
            src: 'https://new.jkhsakha.ru/wp-content/uploads/elementor/thumbs/%D0%A2%D0%BE%D0%BC%D0%BF%D0%BE%D0%BD%D1%81%D0%BA%D0%B8%D0%B9-pxq86rdnceg2hdx8fbvqpmicak6lrwwkvt706wfjrk.jpg'
        },
        {
            id: 24,
            title: 'Усть-Алданский',
            src: 'https://new.jkhsakha.ru/wp-content/uploads/elementor/thumbs/%D0%A3%D1%81%D1%82%D1%8C-%D0%90%D0%BB%D0%B4%D0%B0%D0%BD%D1%81%D0%BA%D0%B8%D0%B9-pxq86w2uakmi3fqenvwvk3bn9hjfuef8kggfla8kwg.jpg'
        },
        {
            id: 25,
            title: 'Хангаласский',
            src: 'https://new.jkhsakha.ru/wp-content/uploads/elementor/thumbs/%D0%A5%D0%B0%D0%BD%D0%B3%D0%B0%D0%BB%D0%B0%D1%81%D1%81%D0%BA%D0%B8%D0%B9-pxq86zu71wrndvky1xjdu2dhn10wp6u5wz2die307k.jpg'
        },
        {
            id: 26,
            title: 'Чурапчинский',
            src: 'https://new.jkhsakha.ru/wp-content/uploads/elementor/thumbs/%D0%A7%D1%83%D1%80%D0%B0%D0%BF%D1%87%D0%B8%D0%BD%D1%81%D0%BA%D0%B8%D0%B9-pxq873ljt8wsobfhfz5w41fc0kidjz939hobfhxfio.jpg'
        },
        {
            id: 27,
            title: 'Эвено-Бытантайский',
            src: 'https://new.jkhsakha.ru/wp-content/uploads/elementor/thumbs/%D0%AD%D0%B2%D0%B5%D0%BD%D0%BE-%D0%91%D1%8B%D1%82%D0%B0%D0%BD%D1%82%D0%B0%D0%B9%D1%81%D0%BA%D0%B8%D0%B9-pxq877cwkl1xyra0u0see0h6e3zuero0m0a9clruts.jpg'
        }
    ]

    const slides: CarouselSlide[] = [
        {
            id: 0,
            title: "Тепло сердец - людям!",
            src: "/first_slide2.jpg",
        },
        {
            id: 1,
            title: "Личный кабинет",
            src: "/Личный-кабинет.jpg",
            href: "https://lk.jkhsakha.ru",
        },
        {
            id: 2,
            title: "Способы оплаты",
            src: "/Способ-оплаты.jpg",
            href: "/sposoby-oplaty/",
        }
    ]

    return (
        <div className="w-full flex flex-col items-center sm:gap-8">
            {/* {posts.map(post => (
                <div key={post.ID} className="max-w-5xl">
                    {renderPostContent(post.post_content)}
                </div>
            ))} */}

            <EmblaCarousel autoplay title_align='left' title_position='top' show_dots={true} show_title={true} slides={slides} />

            <Section title='Новости'>
                <Grid gap={4} cols_mobile={1} cols_tablet={1} cols_desktop={2} rows_mobile={1} rows_tablet={1} rows_desktop={1}>
                    <EmblaCarousel rounded title_align='left' shape='square' show_title title_position='bottom' autoplay={true} show_dots={true} slides_per_view={1} slides={slides} dots_position='bottom_inside' />

                    <Flex justify='between' direction='col' gap={4}>
                        <PostCard
                            thumbnail_src='https://new.jkhsakha.ru/wp-content/uploads/2026/02/1-1-300x179.jpeg'
                            post_href='/#'
                            description='Post card test'
                            title='Работники ЖКХ – обладатели Гран-при военно-патриотического конкурса'
                            show_thumbnail={true}
                            show_categories
                            categories={[
                                {
                                    id: 0,
                                    slug: 'news',
                                    name: 'Новости'
                                },
                                {
                                    id: 1,
                                    slug: 'news',
                                    name: 'Новости'
                                }
                            ]}
                            date='26.02.2026'
                            show_date
                        />
                        <PostCard
                            post_href='/#'
                            title='Осторожно! Мошенники рассылают СМС о возврате за отопление'
                            description='asd'
                        />
                        <PostCard
                            post_href='/#'
                            title='Профсоюз ГУП «ЖКХ РС(Я)» поздравил ветеранов с наступающим Новым годом'

                        />
                    </Flex>
                </Grid>
            </Section>

            <Section title='О предприятии'>
                <Grid cols_mobile={1} cols_tablet={1} cols_desktop={2} rows_mobile={1} rows_tablet={1} rows_desktop={1}>
                    <Areas />


                    <Grid cols_desktop={2} cols_mobile={2} cols_tablet={2} gap={4}>
                        {stats.map((stat, index) => (
                            <Stat key={index} value={stat.value} title={stat.title} />
                        ))}
                    </Grid>
                </Grid>
            </Section>

            <Section title='Филиалы'>
                <EmblaCarousel rounded autoscroll={true} show_dots={false} slides_per_view={4} slides={areaSlides} dots_position='bottom' />
            </Section>

            <Section title="Контакты">

            </Section>

            <IFrame iframe={`<iframe loading="lazy"
					src="https://maps.google.com/maps?q=%D0%B3.%20%D0%AF%D0%BA%D1%83%D1%82%D1%81%D0%BA%2C%20%D1%83%D0%BB.%20%D0%9A%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%2C%20%D0%B4.18%2C%20%D0%B1%D0%BB%D0%BE%D0%BA%20%22%D0%90%22&#038;t=m&#038;z=16&#038;output=embed&#038;iwloc=near"
					title="г. Якутск, ул. Кирова, д.18, блок &quot;А&quot;"
					aria-label="г. Якутск, ул. Кирова, д.18, блок &quot;А&quot;"
			></iframe>`} />
        </div>
    );
}
