import { useContext, useEffect, useState } from "react";
import {
  addStoreToHousehold,
  getAllStores,
  removeStoreFromHousehold,
  setDefaultStore
} from "../../api/stores";
import { HouseholdContext } from "../../context/HouseholdContext";
import { StoreContext } from "../../context/StoreContext";
import "../../styles/components/manage/ManageStores.css";

export default function ManageStores() {
  const { activeHousehold } = useContext(HouseholdContext);
  const { stores: householdStores, refreshStores } = useContext(StoreContext);
  const [allStores, setAllStores] = useState([]);
  const [loading, setLoading] = useState(true);
  const [showAddStore, setShowAddStore] = useState(false);

  const isAdmin = activeHousehold?.role === "admin";

  useEffect(() => {
    loadAllStores();
  }, []);

  const loadAllStores = async () => {
    setLoading(true);
    try {
      const response = await getAllStores();
      setAllStores(response.data);
    } catch (error) {
      console.error("Failed to load stores:", error);
    } finally {
      setLoading(false);
    }
  };

  const handleAddStore = async (storeId) => {
    try {
      console.log("Adding store with ID:", storeId);
      await addStoreToHousehold(activeHousehold.id, storeId, false);
      await refreshStores();
      setShowAddStore(false);
    } catch (error) {
      console.error("Failed to add store:", error);
      alert("Failed to add store");
    }
  };

  const handleRemoveStore = async (storeId, storeName) => {
    if (!confirm(`Remove ${storeName} from this household?`)) return;

    try {
      await removeStoreFromHousehold(activeHousehold.id, storeId);
      await refreshStores();
    } catch (error) {
      console.error("Failed to remove store:", error);
      alert("Failed to remove store");
    }
  };

  const handleSetDefault = async (storeId) => {
    try {
      await setDefaultStore(activeHousehold.id, storeId);
      await refreshStores();
    } catch (error) {
      console.error("Failed to set default store:", error);
      alert("Failed to set default store");
    }
  };

  const availableStores = allStores.filter(
    store => !householdStores.some(hs => hs.id === store.id)
  );

  return (
    <div className="manage-stores">
      {/* Current Stores Section */}
      <section className="manage-section">
        <h2>Your Stores ({householdStores.length})</h2>
        {householdStores.length === 0 ? (
          <p className="empty-message">No stores added yet.</p>
        ) : (
          <div className="stores-list">
            {householdStores.map((store) => (
              <div key={store.id} className="store-card">
                <div className="store-info">
                  <h3>{store.name}</h3>
                  {store.location && <p className="store-location">{store.location}</p>}
                </div>
                {isAdmin && (
                  <div className="store-actions">
                    {!store.is_default && (
                      <button
                        onClick={() => handleSetDefault(store.id)}
                        className="btn-secondary btn-small"
                      >
                        Set as Default
                      </button>
                    )}
                    <button
                      onClick={() => handleRemoveStore(store.id, store.name)}
                      className="btn-danger btn-small"
                      disabled={householdStores.length === 1}
                      title={householdStores.length === 1 ? "Cannot remove last store" : ""}
                    >
                      Remove
                    </button>
                  </div>
                )}
              </div>
            ))}
          </div>
        )}
      </section>

      {/* Add Store Section */}
      {isAdmin && (
        <section className="manage-section">
          <h2>Add Store</h2>
          {!showAddStore ? (
            <button onClick={() => setShowAddStore(true)} className="btn-primary">
              + Add Store
            </button>
          ) : (
            <div className="add-store-panel">
              <button onClick={() => setShowAddStore(false)} className="btn-secondary">
                Cancel
              </button>
              {loading ? (
                <p>Loading stores...</p>
              ) : availableStores.length === 0 ? (
                <p className="empty-message">All available stores have been added.</p>
              ) : (
                <div className="available-stores">
                  {availableStores.map((store) => (
                    <div key={store.id} className="available-store-card">
                      <div className="store-info">
                        <h3>{store.name}</h3>
                        {store.location && <p className="store-location">{store.location}</p>}
                      </div>
                      <button
                        onClick={() => handleAddStore(store.id)}
                        className="btn-primary btn-small"
                      >
                        Add
                      </button>
                    </div>
                  ))}
                </div>
              )}
            </div>
          )}
        </section>
      )}
    </div>
  );
}
