import React, { useState, useEffect } from 'react';
import AdCard from '../components/AdCard';
import AdForm from '../components/AdForm';
import { fetchAds, addAd } from '../utils/api';

const ManageAds = () => {
  const [ads, setAds] = useState([]);

  useEffect(() => {
    const getAds = async () => {
      const adsData = await fetchAds();
      setAds(adsData);
    };
    getAds();
  }, []);

  const handleAddAd = async (ad) => {
    await addAd(ad);
    setAds([...ads, ad]);
  };

  return (
    <div>
      <h1>Manage Ads</h1>
      <AdForm onAddAd={handleAddAd} />
      <div>
        {ads.map((ad) => (
          <AdCard key={ad.id} ad={ad} />
        ))}
      </div>
    </div>
  );
};

export default ManageAds;
