import React, { useState, useEffect } from 'react';
import { Link } from 'react-router-dom';
import { useAuth } from '../contexts/AuthContext';
import { choreService, Chore } from '../api/chores';
import ChoreCard from '../components/ChoreCard';
import CreateChoreModal from '../components/CreateChoreModal';
import EditChoreModal from '../components/EditChoreModal';
import api from '../api/axios';

interface User {
  id: number;
  username: string;
  full_name: string;
  is_active: boolean;
}

const Dashboard: React.FC = () => {
  const { user, logout } = useAuth();
  const [chores, setChores] = useState<Chore[]>([]);
  const [users, setUsers] = useState<User[]>([]);
  const [isLoading, setIsLoading] = useState(true);
  const [showCreateModal, setShowCreateModal] = useState(false);
  const [editingChoreId, setEditingChoreId] = useState<number | null>(null);
  const [filter, setFilter] = useState<'all' | 'my' | 'today'>('all');
  const [selectedUserId, setSelectedUserId] = useState<number | null>(null);
  const [hideBirthdayChores, setHideBirthdayChores] = useState(false);

  useEffect(() => {
    loadData();
  }, [selectedUserId, hideBirthdayChores]);

  const loadData = async () => {
    setIsLoading(true);
    try {
      const [choresData, usersData] = await Promise.all([
        choreService.getChores({
          user_id: selectedUserId || undefined,
          exclude_birthdays: hideBirthdayChores
        }),
        api.get<User[]>('/api/v1/users')
      ]);
      
      setChores(choresData);
      setUsers(usersData.data.filter(u => u.is_active));
    } catch (error) {
      console.error('Failed to load data:', error);
    } finally {
      setIsLoading(false);
    }
  };

  const handleCompleteChore = async (id: number) => {
    try {
      await choreService.completeChore(id);
      await loadData();
    } catch (error) {
      console.error('Failed to complete chore:', error);
    }
  };

  const handleDeleteChore = async (id: number) => {
    if (window.confirm('Are you sure you want to delete this chore?')) {
      try {
        await choreService.deleteChore(id);
        await loadData();
      } catch (error) {
        console.error('Failed to delete chore:', error);
      }
    }
  };

  const handleEditChore = (id: number) => {
    setEditingChoreId(id);
  };

  const filteredChores = chores.filter((chore) => {
    if (filter === 'my') {
      return chore.assigned_users?.some(u => u.id === user?.id);
    }
    if (filter === 'today') {
      const today = new Date().toISOString().split('T')[0];
      return chore.due_date?.startsWith(today) || chore.frequency === 'daily';
    }
    return true;
  });

  // Calculate stats
  const todayChores = chores.filter((chore) => {
    const today = new Date().toISOString().split('T')[0];
    const isToday = chore.due_date?.startsWith(today) || chore.frequency === 'daily';
    const notCompleted = chore.status !== 'completed';
    return isToday && notCompleted;
  });

  const myChores = chores.filter((chore) => 
    chore.assigned_users?.some(u => u.id === user?.id) && chore.status !== 'completed'
  );

  const totalPoints = filteredChores.reduce((sum, chore) => 
    chore.status !== 'completed' ? sum + chore.points : sum, 0
  );

  const myPoints = myChores.reduce((sum, chore) => sum + chore.points, 0);

  return (
    <div className="min-h-screen bg-gray-50">
      {/* Header */}
      <header className="bg-white shadow-sm">
        <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-4 flex justify-between items-center">
          <div>
            <h1 className="text-2xl font-bold text-gray-900">Family Hub</h1>
            <p className="text-sm text-gray-600">Welcome back, {user?.full_name}!</p>
          </div>
          <div className="flex items-center gap-3">
            <Link
              to="/settings"
              className="px-4 py-2 text-sm text-gray-700 hover:text-gray-900 hover:bg-gray-100 rounded-lg transition-colors flex items-center gap-2"
            >
              <svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z" />
                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
              </svg>
              Settings
            </Link>
            <button
              onClick={logout}
              className="px-4 py-2 text-sm text-gray-700 hover:text-gray-900 hover:bg-gray-100 rounded-lg transition-colors"
            >
              Sign Out
            </button>
          </div>
        </div>
      </header>

      {/* Main Content */}
      <main className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
        {/* Stats */}
        <div className="grid grid-cols-1 md:grid-cols-4 gap-6 mb-8">
          <div className="bg-white rounded-lg shadow p-6">
            <div className="flex items-center justify-between">
              <div>
                <p className="text-sm text-gray-600">Today's Tasks</p>
                <p className="text-3xl font-bold text-blue-600">{todayChores.length}</p>
              </div>
              <div className="p-3 bg-blue-100 rounded-full">
                <svg className="w-8 h-8 text-blue-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2" />
                </svg>
              </div>
            </div>
          </div>

          <div className="bg-white rounded-lg shadow p-6">
            <div className="flex items-center justify-between">
              <div>
                <p className="text-sm text-gray-600">My Tasks</p>
                <p className="text-3xl font-bold text-green-600">{myChores.length}</p>
              </div>
              <div className="p-3 bg-green-100 rounded-full">
                <svg className="w-8 h-8 text-green-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z" />
                </svg>
              </div>
            </div>
          </div>

          <div className="bg-white rounded-lg shadow p-6">
            <div className="flex items-center justify-between">
              <div>
                <p className="text-sm text-gray-600">My Points</p>
                <p className="text-3xl font-bold text-amber-600">{myPoints}</p>
              </div>
              <div className="p-3 bg-amber-100 rounded-full">
                <span className="text-3xl">⭐</span>
              </div>
            </div>
          </div>

          <div className="bg-white rounded-lg shadow p-6">
            <div className="flex items-center justify-between">
              <div>
                <p className="text-sm text-gray-600">Total Available</p>
                <p className="text-3xl font-bold text-purple-600">{totalPoints}</p>
              </div>
              <div className="p-3 bg-purple-100 rounded-full">
                <svg className="w-8 h-8 text-purple-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M19 11H5m14 0a2 2 0 012 2v6a2 2 0 01-2 2H5a2 2 0 01-2-2v-6a2 2 0 012-2m14 0V9a2 2 0 00-2-2M5 11V9a2 2 0 012-2m0 0V5a2 2 0 012-2h6a2 2 0 012 2v2M7 7h10" />
                </svg>
              </div>
            </div>
          </div>
        </div>

        {/* Filters and Actions */}
        <div className="flex flex-col sm:flex-row justify-between items-start sm:items-center gap-4 mb-6">
          <div className="flex flex-wrap gap-2">
            <button
              onClick={() => { setFilter('all'); setSelectedUserId(null); }}
              className={`px-4 py-2 rounded-lg transition-colors ${
                filter === 'all' && !selectedUserId
                  ? 'bg-blue-600 text-white'
                  : 'bg-white text-gray-700 hover:bg-gray-50'
              }`}
            >
              All Tasks
            </button>
            <button
              onClick={() => { setFilter('today'); setSelectedUserId(null); }}
              className={`px-4 py-2 rounded-lg transition-colors ${
                filter === 'today'
                  ? 'bg-blue-600 text-white'
                  : 'bg-white text-gray-700 hover:bg-gray-50'
              }`}
            >
              Today
            </button>
            <button
              onClick={() => { setFilter('my'); setSelectedUserId(null); }}
              className={`px-4 py-2 rounded-lg transition-colors ${
                filter === 'my'
                  ? 'bg-blue-600 text-white'
                  : 'bg-white text-gray-700 hover:bg-gray-50'
              }`}
            >
              My Tasks
            </button>

            {/* User Filter Dropdown */}
            <select
              value={selectedUserId || ''}
              onChange={(e) => {
                setSelectedUserId(e.target.value ? parseInt(e.target.value) : null);
                setFilter('all');
              }}
              className="px-4 py-2 bg-white border border-gray-300 rounded-lg text-gray-700 hover:bg-gray-50 focus:ring-2 focus:ring-blue-500"
            >
              <option value="">Filter by User...</option>
              {users.map(u => (
                <option key={u.id} value={u.id}>{u.full_name}</option>
              ))}
            </select>

            {/* Birthday Filter Toggle */}
            <button
              onClick={() => setHideBirthdayChores(!hideBirthdayChores)}
              className={`px-4 py-2 rounded-lg transition-colors flex items-center gap-2 ${
                hideBirthdayChores
                  ? 'bg-purple-600 text-white'
                  : 'bg-white text-gray-700 hover:bg-gray-50 border border-gray-300'
              }`}
            >
              <span>🎂</span>
              <span>Hide Birthday Chores</span>
            </button>
          </div>

          <button
            onClick={() => setShowCreateModal(true)}
            className="px-4 py-2 bg-green-600 text-white rounded-lg hover:bg-green-700 transition-colors flex items-center gap-2 whitespace-nowrap"
          >
            <svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
              <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 4v16m8-8H4" />
            </svg>
            Create Task
          </button>
        </div>

        {/* Active Filters Display */}
        {(selectedUserId || hideBirthdayChores) && (
          <div className="mb-4 flex flex-wrap gap-2">
            {selectedUserId && (
              <div className="inline-flex items-center gap-2 px-3 py-1 bg-blue-100 text-blue-800 rounded-full text-sm">
                <span>User: {users.find(u => u.id === selectedUserId)?.full_name}</span>
                <button
                  onClick={() => setSelectedUserId(null)}
                  className="hover:bg-blue-200 rounded-full p-0.5"
                >
                  <svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
                  </svg>
                </button>
              </div>
            )}
            {hideBirthdayChores && (
              <div className="inline-flex items-center gap-2 px-3 py-1 bg-purple-100 text-purple-800 rounded-full text-sm">
                <span>🎂 Hiding birthday chores</span>
                <button
                  onClick={() => setHideBirthdayChores(false)}
                  className="hover:bg-purple-200 rounded-full p-0.5"
                >
                  <svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
                  </svg>
                </button>
              </div>
            )}
          </div>
        )}

        {/* Chores List */}
        {isLoading ? (
          <div className="text-center py-12">
            <div className="inline-block animate-spin rounded-full h-12 w-12 border-b-2 border-blue-600"></div>
            <p className="mt-4 text-gray-600">Loading chores...</p>
          </div>
        ) : filteredChores.length === 0 ? (
          <div className="text-center py-12 bg-white rounded-lg shadow">
            <svg className="mx-auto h-12 w-12 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
              <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2" />
            </svg>
            <h3 className="mt-2 text-sm font-medium text-gray-900">No chores found</h3>
            <p className="mt-1 text-sm text-gray-500">
              {selectedUserId 
                ? "This user has no assigned chores."
                : hideBirthdayChores
                ? "All chores are birthday chores today! 🎂"
                : "Get started by creating a new chore."}
            </p>
          </div>
        ) : (
          <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
            {filteredChores.map((chore) => (
              <ChoreCard
                key={chore.id}
                chore={chore}
                onComplete={handleCompleteChore}
                onDelete={handleDeleteChore}
                onEdit={handleEditChore}
              />
            ))}
          </div>
        )}
      </main>

      {/* Modals */}
      {showCreateModal && (
        <CreateChoreModal
          onClose={() => setShowCreateModal(false)}
          onSuccess={() => {
            setShowCreateModal(false);
            loadData();
          }}
        />
      )}

      {editingChoreId && (
        <EditChoreModal
          choreId={editingChoreId}
          onClose={() => setEditingChoreId(null)}
          onSuccess={() => {
            setEditingChoreId(null);
            loadData();
          }}
        />
      )}
    </div>
  );
};

export default Dashboard;
