'use client';

import { useEffect, useState } from 'react';
import Link from 'next/link';
import { Card } from '@/components/ui/card';
import { Badge } from '@/components/ui/badge';
import { Skeleton } from '@/components/ui/skeleton';
import { BookOpen } from 'lucide-react';
import { API_BASE } from '@/lib/config';
import { useT } from '@/i18n';

interface Course { id: number; title: string; description: string; cover: string | null; isFree: boolean; chapters?: { lessons: any[] }[] }

function CourseSkeleton() {
  return (
    <Card className="p-6">
      <Skeleton className="h-40 w-full rounded-lg mb-4" />
      <Skeleton className="h-4 w-16 mb-3" />
      <Skeleton className="h-5 w-3/4 mb-2" />
      <Skeleton className="h-4 w-full" />
    </Card>
  );
}

export default function CoursesPage() {
  const t = useT();
  const [courses, setCourses] = useState<Course[]>([]);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    fetch(`${API_BASE}/courses`)
      .then(r => r.json()).then(data => setCourses(data.items || []))
      .catch(() => {}).finally(() => setLoading(false));
  }, []);

  return (
    <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
      <div className="mb-10">
        <h1 className="text-3xl font-bold text-foreground">{t.courses.title}</h1>
        <p className="mt-2 text-muted-foreground">{t.courses.desc}</p>
      </div>

      {loading ? (
        <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">{[1,2,3,4,5,6].map(i => <CourseSkeleton key={i} />)}</div>
      ) : courses.length === 0 ? (
        <div className="text-center py-20 text-muted-foreground">
          <BookOpen className="w-12 h-12 mx-auto mb-4 opacity-30" />
          <p>{t.courses.empty}</p>
        </div>
      ) : (
        <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
          {courses.map((course) => (
            <Link key={course.id} href={`/courses/${course.id}`} className="block group">
              <Card className="overflow-hidden hover:shadow-lg transition-all hover:-translate-y-0.5">
                {course.cover ? (
                  <img src={course.cover} alt={course.title} className="w-full h-40 object-cover" />
                ) : (
                  <div className="h-40 bg-gradient-to-br from-brand-50 to-blue-50 dark:from-brand-950/30 dark:to-blue-950/20 flex items-center justify-center">
                    <BookOpen className="w-10 h-10 text-brand-300 dark:text-brand-600" />
                  </div>
                )}
                <div className="p-5">
                  <Badge variant={course.isFree ? 'secondary' : 'destructive'} className="mb-3">
                    {course.isFree ? t.courses.free : t.courses.paid}
                  </Badge>
                  <h3 className="font-semibold group-hover:text-brand-600 transition-colors mb-2">{course.title}</h3>
                  <p className="text-sm text-muted-foreground line-clamp-2">{course.description}</p>
                  {course.chapters && (
                    <div className="flex items-center gap-2 mt-3 text-xs text-muted-foreground">
                      <BookOpen className="w-3.5 h-3.5" />
                      <span>{t.courses.moduleCount.replace('{n}', String(course.chapters.reduce((s, ch) => s + (ch.lessons?.length || 0), 0)))}</span>
                    </div>
                  )}
                </div>
              </Card>
            </Link>
          ))}
        </div>
      )}
    </div>
  );
}
