'use client'
import Image from 'next/image'
import Link from 'next/link'
import { FC, useMemo } from 'react'

import { IBanner } from '@/types/landing.types'
import { useGetLanding } from '@/app/home/hooks/useHomeData'

const Banners3: FC = () => {

    const { data } = useGetLanding()

    const { desktopBanners, mobileBanners } = useMemo(() => {
        const allBanners = data?.results?.banners ?? []

        const groupByDisplayLocation = (displayLocation: IBanner['displayLocation']) =>
            allBanners.filter((banner) => banner.displayLocation === displayLocation)

        return {
            desktopBanners: groupByDisplayLocation('desktop'),
            mobileBanners: groupByDisplayLocation('mobile'),
        }
    }, [data?.results?.banners])

    const renderLayout = (banners: IBanner[]) => {
        const firstBanner = banners.find((banner) => banner.order === 1)
        const secondBanner = banners.find((banner) => banner.order === 2)
        const thirdBanner = banners.find((banner) => banner.order === 3)

        if (!firstBanner?.imageUrl || !secondBanner?.imageUrl || !thirdBanner?.imageUrl) return null

        return (
            <div className='flex lg:flex-row flex-col gap-4'>
                <div className='flex flex-col gap-2 h-full flex-1'>
                    <div className='flex-1 h-full'>
                        <Link href={firstBanner.linkUrl || '#'}>
                            <Image
                                src={firstBanner.imageUrl}
                                height={500}
                                width={1000}
                                className='flex-1 rounded-lg'
                                alt={firstBanner.altText}
                            />
                        </Link>
                    </div>
                    <div className='flex-1 h-full'>
                        <Link href={secondBanner.linkUrl || '#'}>
                            <Image
                                src={secondBanner.imageUrl}
                                height={500}
                                width={1000}
                                className='flex-1 rounded-lg'
                                alt={secondBanner.altText}
                            />
                        </Link>
                    </div>
                </div>
                <div className='flex-1 h-full'>
                    <Link href={thirdBanner.linkUrl || '#'}>
                        <Image
                            src={thirdBanner.imageUrl}
                            height={1000}
                            width={1000}
                            className='flex-1 rounded-lg h-full object-cover'
                            alt={thirdBanner.altText}
                        />
                    </Link>
                </div>
            </div>
        )
    }

    return (
        <>
            {desktopBanners.length > 0 && (
                <div className='hidden lg:block'>{renderLayout(desktopBanners)}</div>
            )}
            {mobileBanners.length > 0 && (
                <div className='lg:hidden'>{renderLayout(mobileBanners)}</div>
            )}
        </>
    )
}

export default Banners3