import {useState, useEffect} from 'react';
import type {Artwork} from '@/types';

const SAMPLE_ARTWORKS: Artwork[] = [
  {
    id: '1',
    title: 'Dragon 1',
    description: '',
    imageUrl: '/gallery/ArtFixture_2.jpg?inline',
  },
  {
    id: '2',
    title: 'Dragon 2',
    description: '',
    imageUrl: '/gallery/ArtFixture_3.jpg?inline',
  },
  {
    id: '3',
    title: 'Dragon 3',
    description: '',
    imageUrl: '/gallery/ArtFixture_4.jpg?inline',
  },
  {
    id: '4',
    title: 'Abstract Emotions',
    description: '',
    imageUrl: '/gallery/ArtFixture_5.jpg?inline',
  },
  {
    id: '5',
    title: 'Humorous',
    description: '',
    imageUrl: '/gallery/ArtFixture_6.png?inline',
  },
  {
    id: '6',
    title: 'Rainbows',
    description: '',
    imageUrl: '/gallery/ArtFixture_7.jpg?inline',
  }
];

export const useArtwork = () => {
  const [artworks, setArtworks] = useState<Artwork[]>([]);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    // Load from localStorage or use sample data
    const stored = localStorage.getItem('artworks');
    if (stored) {
      setArtworks(JSON.parse(stored));
    } else {
      setArtworks(SAMPLE_ARTWORKS);
      localStorage.setItem('artworks', JSON.stringify(SAMPLE_ARTWORKS));
    }
    setLoading(false);
  }, []);

  const addArtwork = (artwork: Omit<Artwork, 'id'>) => {
    const newArtwork: Artwork = {
      ...artwork,
      id: Date.now().toString()
    };
    const updated = [...artworks, newArtwork];
    setArtworks(updated);
    localStorage.setItem('artworks', JSON.stringify(updated));
  };

  const deleteArtwork = (id: string) => {
    const updated = artworks.filter(art => art.id !== id);
    setArtworks(updated);
    localStorage.setItem('artworks', JSON.stringify(updated));
  };

  return {
    artworks,
    loading,
    addArtwork,
    deleteArtwork
  };
};
