"use client";

import { useAuth } from "@/lib/auth-context";
import { mockReports } from "@/lib/mock-data";
import {
  Card,
  CardContent,
  CardHeader,
  CardTitle,
  CardDescription,
} from "@/components/ui/card";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import { DashboardBreadcrumb } from "@/components/dashboard/dashboard-breadcrumb";
import Link from "next/link";
import {
  Plus,
  FileText,
  Calendar,
  Users,
  MapPin,
  IndianRupee,
  TrendingUp,
} from "lucide-react";

export default function ReportsPage() {
  const { user } = useAuth();
  const reports = mockReports.filter((r) => r.guideId === user?.guideId);

  const totalTours = reports.reduce((acc, r) => acc + r.toursCount, 0);
  const totalTourists = reports.reduce((acc, r) => acc + r.touristsServed, 0);
  const totalRevenue = reports.reduce((acc, r) => acc + r.revenue, 0);

  return (
    <div className="space-y-5">
      <DashboardBreadcrumb current="Monthly Reports" />

      <div className="flex flex-col gap-3 rounded-3xl border border-[var(--theme-border)] bg-white p-5 shadow-sm sm:flex-row sm:items-center sm:justify-between">
        <div>
          <h1 className="text-2xl font-black text-foreground">
            Monthly Reports
          </h1>
          <p className="mt-1 text-sm font-medium text-muted-foreground">
            Track and submit your monthly tour activities
          </p>
        </div>
        <Link href="/dashboard/reports/new">
          <Button>
            <Plus className="h-4 w-4 mr-2" />
            Submit Report
          </Button>
        </Link>
      </div>

      {/* Summary Stats */}
      <div className="grid gap-4 sm:grid-cols-3">
        <Card className="rounded-3xl border-[var(--theme-border)] bg-white py-0 shadow-sm">
          <CardContent className="p-5">
            <div className="flex items-center justify-between">
              <div>
                <p className="text-sm font-semibold text-muted-foreground">
                  Total Tours
                </p>
                <p className="mt-3 text-3xl font-black text-foreground">
                  {totalTours}
                </p>
              </div>
              <div className="flex h-12 w-12 items-center justify-center rounded-2xl bg-[var(--theme-surface-strong)] text-[var(--theme-primary)]">
                <TrendingUp className="h-6 w-6" />
              </div>
            </div>
          </CardContent>
        </Card>
        <Card className="rounded-3xl border-[var(--theme-border)] bg-white py-0 shadow-sm">
          <CardContent className="p-5">
            <div className="flex items-center justify-between">
              <div>
                <p className="text-sm font-semibold text-muted-foreground">
                  Tourists Served
                </p>
                <p className="mt-3 text-3xl font-black text-foreground">
                  {totalTourists}
                </p>
              </div>
              <div className="flex h-12 w-12 items-center justify-center rounded-2xl bg-[var(--theme-surface-strong)] text-[var(--theme-primary)]">
                <Users className="h-6 w-6" />
              </div>
            </div>
          </CardContent>
        </Card>
        <Card className="rounded-3xl border-[var(--theme-border)] bg-white py-0 shadow-sm">
          <CardContent className="p-5">
            <div className="flex items-center justify-between">
              <div>
                <p className="text-sm font-semibold text-muted-foreground">
                  Total Revenue
                </p>
                <p className="mt-3 text-3xl font-black text-foreground">
                  <IndianRupee className="h-5 w-5 inline" />
                  {totalRevenue.toLocaleString()}
                </p>
              </div>
              <div className="flex h-12 w-12 items-center justify-center rounded-2xl bg-[var(--theme-surface-strong)] text-[var(--theme-primary)]">
                <IndianRupee className="h-6 w-6" />
              </div>
            </div>
          </CardContent>
        </Card>
      </div>

      {/* Reports List */}
      {reports.length > 0 ? (
        <div className="space-y-4">
          {reports.map((report) => {
            const monthDate = new Date(report.month + "-01");
            const monthName = monthDate.toLocaleDateString("en-IN", {
              month: "long",
              year: "numeric",
            });

            return (
              <Card
                key={report.id}
                className="rounded-3xl border-[var(--theme-border)] bg-white py-0 shadow-sm transition-all hover:-translate-y-0.5 hover:shadow-lg hover:shadow-[var(--theme-secondary)]/10"
              >
                <CardContent className="p-6">
                  <div className="flex flex-col sm:flex-row sm:items-center justify-between gap-4">
                    <div className="flex items-start gap-4">
                      <div className="flex h-12 w-12 items-center justify-center rounded-2xl bg-[var(--theme-surface-strong)]">
                        <Calendar className="h-6 w-6 text-[var(--theme-primary)]" />
                      </div>
                      <div>
                        <h3 className="font-semibold text-foreground">
                          {monthName}
                        </h3>
                        <div className="flex flex-wrap gap-x-4 gap-y-1 text-sm text-muted-foreground mt-1">
                          <span className="flex items-center gap-1">
                            <TrendingUp className="h-3.5 w-3.5" />
                            {report.toursCount} tours
                          </span>
                          <span className="flex items-center gap-1">
                            <Users className="h-3.5 w-3.5" />
                            {report.touristsServed} tourists
                          </span>
                          <span className="flex items-center gap-1">
                            <IndianRupee className="h-3.5 w-3.5" />
                            {report.revenue.toLocaleString()}
                          </span>
                        </div>
                      </div>
                    </div>
                    <Badge
                      variant={
                        report.status === "reviewed" ? "default" : "secondary"
                      }
                    >
                      {report.status === "reviewed" ? "Reviewed" : "Submitted"}
                    </Badge>
                  </div>

                  <div className="mt-4 border-t border-[var(--theme-border)] pt-4">
                    <p className="text-sm text-muted-foreground mb-2">
                      <MapPin className="h-3.5 w-3.5 inline mr-1" />
                      <span className="font-medium">Service Locations:</span>{" "}
                      {report.locations.join(", ")}
                    </p>
                    <p className="text-sm text-muted-foreground">
                      <span className="font-medium">Feedback:</span>{" "}
                      {report.feedback}
                    </p>
                  </div>
                </CardContent>
              </Card>
            );
          })}
        </div>
      ) : (
        <Card className="rounded-3xl border-[var(--theme-border)] bg-white py-0 shadow-sm">
          <CardContent className="py-16 text-center">
            <FileText className="h-12 w-12 mx-auto mb-4 text-muted-foreground" />
            <h3 className="text-lg font-semibold text-foreground mb-2">
              No reports submitted yet
            </h3>
            <p className="text-muted-foreground mb-6">
              Submit monthly reports to track your tour activities
            </p>
            <Link href="/dashboard/reports/new">
              <Button>
                <Plus className="h-4 w-4 mr-2" />
                Submit Your First Report
              </Button>
            </Link>
          </CardContent>
        </Card>
      )}
    </div>
  );
}
