'use client';

import RewardNewEditForm from '@/shared/sections/rewards/add-reward/reward-new-edit-form';
import { useEffect, useState } from 'react';
import { IRewardItem } from '@/contexts/types/reward';
import axiosInstancee from '@/utils/axios';
import { LoadingScreen } from '@/shared/components/loading-screen';
import { NotFoundView } from '@/shared/sections/error';

type Props = {
  params: {
    id: string;
  };
};

export default function RewardEditPage({ params }: Props) {
  const { id } = params;
  const [reward, setReward] = useState<IRewardItem | null>(null);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    const fetchReward = async () => {
      try {
        const response = await axiosInstancee.get(`/api/projects/rewards/${id}`);
        setReward(response.data);
      } catch (error) {
        console.error('Error fetching reward:', error);
        setReward(null);
      } finally {
        setLoading(false);
      }
    };

    fetchReward();
  }, [id]);

  if (loading) {
    return <LoadingScreen />;
  }

  if (!reward) {
    return <NotFoundView />;
  }

  return <RewardNewEditForm currentReward={reward} />;
} 