import { useEffect, useState } from "react";
import ArticleCard from "./ArticleCard";
import { ArticlePreview } from "../types";

export default function ArticlesSection() {

    const [articlePreviews, setArticlePreviews] = useState<ArticlePreview[]>([]);
    const [loading, setLoading] = useState<boolean>(true);
    const [error, setError] = useState<string | null>(null);


    const articles: ArticlePreview[] = [
        {
            id: 1,
            title: "The Great Barrier Reef",
            preview: "The Great Barrier Reef is the largest coral reef system in the world, composed of over 2,900 individual reefs and 900 islands stretching for over 2,300 kilometers (1,400 mi) over an area of approximately 344,400 square kilometers (133,000 sq mi).",        
        },
        {
            id: 2,
            title: "The Great Pacific Garbage Patch",
            preview: "The Great Pacific Garbage Patch, also known as the Pacific trash vortex, is a gyre of marine debris particles in the north-central Pacific Ocean. It is located roughly from 135°W to 155°W and 35°N to 42°N.",
        },
        {
            id: 3,
            title: "The Amazon Rainforest",
            preview: "The Amazon rainforest, alternatively, the Amazon Jungle, also known in English as Amazonia, is a moist broadleaf tropical rainforest in the Amazon biome that covers most of the Amazon basin of South America.",
        },
        {
            id: 4,
            title: "The Arctic",
            preview: "The Arctic is a polar region located at the northernmost part of Earth. The Arctic consists of the Arctic Ocean, adjacent seas, and parts of Alaska (United States), Northern Canada (Canada), Finland, Greenland (Kingdom of Denmark), Iceland, Norway, Russia, and Sweden.",
        },
        {
            id: 5,
            title: "The Antarctic",
            preview: "The Antarctic, is a polar region containing the geographic South Pole and is situated in the Antarctic region of the Southern Hemisphere, almost entirely south of the Antarctic Circle, and is surrounded by the Southern Ocean.",

        }
    ];


    useEffect(() => {
        fetch('/api/articles')
            // .then(response => response.json())
            .then(_ => articles)
            .then((data: ArticlePreview[]) => setArticlePreviews(data))
            .catch(_ => setError('Failed to fetch articles'))
            .finally(() => setLoading(false));
    }, []);

    return (
        <div className="row" id="articles" style={{
            display: 'flex',
            flexWrap: 'wrap',
            justifyContent: 'space-around',
            backgroundColor: 'var(--color-verydarkblue)',
            flex: 1,
        }}>
            <h1 style={{
                color: 'var(--color-yellow)',
                fontFamily: '"Haas Grot Text R Web", "Helvetica Neue", Helvetica, Arial, sans-serif',
                fontSize: '32px',
                fontWeight: '500',
                lineHeight: '30px',
                margin: '20px',
                padding: '10px',
                paddingBottom: '0',
                marginBottom: '10px',
                textAlign: 'center',
                width: '100%',
            }}>Articles</h1>
            <div style={{width: '100%', display: 'flex', justifyContent: 'space-around', flexWrap: 'wrap'}}>

                {articlePreviews.map(articlePreview => (
                    <ArticleCard key={articlePreview.id} articlePreview={articlePreview} />
                ))}
            </div>
            {!loading && !error && articlePreviews.length === 0 && <p style={{color: "var(--color-lightblue)", fontSize: 22}}>No articles found</p>}
            {loading && <p style={{color: "var(--color-lightblue)", fontSize: 22}}>Loading...</p>}
            {error && <p style={{color: "var(--color-lightblue)", fontSize: 22}}>{error}</p>}
        </div>
    )
}
