'use client';
import { PageLayout } from '@/components/common/PageLayout';
import { Layout } from '@/src/components/common/Layout';
import { PortfolioForm } from '@/src/components/portfolios/PortfolioForm';
import { useApiWithToast } from '@/src/hooks/useApiWithToast';
import { PortfolioService } from '@/src/services/PortfolioService';
import { PortfolioFormValues } from '@/src/types/portfolio';
import { Portfolio } from '@prisma/client';

export default function Page() {
  const { handle } = useApiWithToast();

  const handleSubmit = async (data: PortfolioFormValues, shouldNavigateToEditor: boolean) => {
    if (shouldNavigateToEditor) {
      return await handle<Portfolio>(PortfolioService.createPortfolio(data), {
        redirectTo: (data) => `/portfolios/${data.id}`,
      });
    } else {
      return await handle<Portfolio>(PortfolioService.createPortfolio(data), {
        redirectTo: '/dashboard',
      });
    }
  };

  return (
    <Layout>
      <PageLayout title='Add Portfolio'>
        <PortfolioForm onSubmit={handleSubmit} />
      </PageLayout>
    </Layout>
  );
}
