'use client';

import { usePathname, useRouter, useSearchParams } from 'next/navigation';
import Tabs from '@/components/Tabs';

import { useTabActionsStore } from '@/stores/tab-actions/tab-actions.store';
import ReportExpenseTab from '@/components/pages/report/expense/tab/ReportExpenseTab';
import ReportDepreciationTab from '@/components/pages/report/expense/tab/ReportDepreciationTab';

const VALID_TAB_IDS = ['operational-expense', 'depreciation'];

const ReportExpenseTabs = () => {
  const router = useRouter();
  const pathname = usePathname();
  const searchParams = useSearchParams();
  const tabParam = searchParams.get('tab') ?? 'operational-expense';
  const activeTabId = VALID_TAB_IDS.includes(tabParam)
    ? tabParam
    : 'operational-expense';
  const tabActions = useTabActionsStore((state) => state.tabActions);

  const handleTabChange = (tabId: string) => {
    router.push(`${pathname}?tab=${tabId}`);
  };

  const tabs = [
    {
      id: 'operational-expense',
      label: 'Laporan Biaya Operasional',
      content: <ReportExpenseTab tabId={'operational-expense'} />,
    },
    {
      id: 'depreciation',
      label: 'Laporan Depresiasi',
      content: <ReportDepreciationTab tabId={'depreciation'} />,
    },
  ];

  return (
    <section className='w-full'>
      <Tabs
        tabs={tabs}
        variant='boxed'
        activeTabId={activeTabId}
        onTabChange={handleTabChange}
        className={{
          tabHeaderWrapper:
            'justify-between items-center p-3 border-b border-base-content/10',
          tab: 'w-fit',
          content: 'p-0 m-0',
        }}
        sideContent={tabActions[activeTabId] || null}
      />
    </section>
  );
};

export default ReportExpenseTabs;
