import { type FC } from 'react'
import { useTranslation } from 'react-i18next'
import Table from '@/components/Table'
import Filters from '@/components/Filters'
import { Add } from 'iconsax-react'
import Button from '@/components/Button'
import { useGetSchedules, useDeleteSchedule } from './hooks/useScheduleData'
import { useScheduleFilters } from './hooks/useScheduleFilters'
import { getScheduleTableColumns } from './components/ScheduleTableColumns'
import { useScheduleFiltersFields } from './components/ScheduleFiltersFields'
import { Link } from 'react-router-dom'
import { Pages } from '@/config/Pages'

const ScheduleList: FC = () => {
    const { t } = useTranslation('global')
    const {
        filters,
        apiParams,
        handleFiltersChange
    } = useScheduleFilters()

    const { data: schedulesData, isLoading } = useGetSchedules(apiParams)
    const { mutate: deleteSchedule, isPending: isDeleting } = useDeleteSchedule()

    const schedules = schedulesData?.data || []
    const columns = getScheduleTableColumns({
        onDelete: (id: string) => deleteSchedule(id),
        isDeleting,
        t
    })
    const filterFields = useScheduleFiltersFields()

    return (
        <div className='mt-5'>
            <div className='flex justify-between items-center'>
                <h1 className='text-lg font-light'>لیست زمانبندی</h1>
                <Link to={Pages.schedule.create}>
                    <Button className='w-fit px-6'>
                        <div className='flex gap-2 items-center'>
                            <Add color='#fff' size={20} />
                            <span>افزودن زمانبندی</span>
                        </div>
                    </Button>
                </Link>
            </div>

            <div className='mt-8'>
                <Filters
                    fields={filterFields}
                    onChange={handleFiltersChange}
                    initialValues={filters}
                    searchField="search"
                />
            </div>

            <div className='bg-yellow-100 text-yellow-500 text-sm p-4 rounded-lg mt-5 w-fit mx-auto font-bold'>
                نکته :
                در صورتی زمان بندی یک روز هفته را وارد نکنید در تمامی ساعت ها امکان سفارش گیری می باشد.
            </div>

            <Table
                columns={columns}
                data={schedules}
                isloading={isLoading}
            />
        </div>
    )
}

export default ScheduleList