import { headers as getHeaders } from 'next/headers.js'
import { getPayload, PaginatedDocs } from 'payload'
import config from '@/payload.config'
import React from 'react'

import UserFeed from '@/components/Feed/UserFeed'
import { Book, Checkout, Repository } from '@/payload-types'
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'
import { LoginForm } from '@/components/login-form'
import SearchBooks from '@/components/Search/SearchBooks'
import Manage from '@/components/Manage/Manage'
import HomeHero from '@/components/HomeHero'

export default async function HomePage() {
  const headers = await getHeaders()
  const payloadConfig = await config
  const payload = await getPayload({ config: payloadConfig })
  const { user } = await payload.auth({ headers })

  const initBrowseBooks = (await payload.find({
    collection: 'books',
    depth: 10,
    limit: 25,
    overrideAccess: false,
    select: {
      title: true,
      authors: true,
      publication: true,
      lcc: true,
      genre: true,
      isbn: true,
      copies: true,
    },
  })) as PaginatedDocs<Book>

  let userRepos: PaginatedDocs<Repository> | null = null
  if (user?.id)
    userRepos = (await payload.find({
      collection: 'repositories',
      depth: 3,
      limit: 10,
      select: {
        name: true,
        abbreviation: true,
        image: true,
        description: true,
        dateOpenToPublic: true,
        holdRequests: true,
      },
      where: {
        'owner.id': {
          equals: user.id,
        },
      },
      joins: {
        holdRequests: {
          limit: 100,
        },
      },
    })) as PaginatedDocs<Repository>

  let userCheckouts: PaginatedDocs<Checkout> | null = null
  if (user?.id)
    userCheckouts = await payload.find({
      collection: 'checkouts',
      depth: 3,
      limit: 10,
      select: {
        id: true,
        copy: true,
        dateDue: true,
      },
      sort: 'dateDue',
      where: {
        and: [
          {
            isReturned: {
              not_equals: true,
            },
          },
          {
            'user.id': {
              equals: user.id,
            },
          },
        ],
      },
    })

  return (
    <div className="home">
      <HomeHero user={user} />

      {user ? (
        <Tabs id="tabs" defaultValue="feed" className="p-4">
          <TabsList className="grid w-full grid-cols-3">
            <TabsTrigger value="feed">Your Feed</TabsTrigger>
            <TabsTrigger value="search">Search</TabsTrigger>
            <TabsTrigger value="manage">Manage</TabsTrigger>
          </TabsList>

          <TabsContent value="feed">
            {user && <UserFeed user={user} repos={userRepos} />}
          </TabsContent>

          <TabsContent value="search">
            <SearchBooks initBrowseBooks={initBrowseBooks} />
          </TabsContent>

          <TabsContent value="manage">
            <Manage repos={userRepos} checkouts={userCheckouts} />
          </TabsContent>
        </Tabs>
      ) : (
        <div className="flex w-full max-w-sm flex-col gap-6 mx-auto my-6">
          <LoginForm />
        </div>
      )}
    </div>
  )
}
