import { ethers } from 'ethers';
import VotingABI from '../contracts/Voting.json';

// Adresse du contrat déployé (à remplacer par votre adresse réelle après déploiement)
export const CONTRACT_ADDRESS = "0x5FbDB2315678afecb367f032d93F642f64180aa3";

export const connectWallet = async () => {
  try {
    if (!window.ethereum) {
      throw new Error("MetaMask n'est pas installé. Veuillez installer MetaMask.");
    }

    const accounts = await window.ethereum.request({
      method: "eth_requestAccounts",
    });

    const provider = new ethers.providers.Web3Provider(window.ethereum);
    const signer = provider.getSigner();
    
    const contract = new ethers.Contract(
      CONTRACT_ADDRESS,
      VotingABI.abi,
      signer
    );

    return { accounts, contract, provider, signer };
  } catch (error) {
    throw error;
  }
};

export const getWorkflowStatusString = (status) => {
  const statusLabels = [
    "Enregistrement des électeurs",
    "Enregistrement des propositions démarré",
    "Enregistrement des propositions terminé",
    "Session de vote démarrée",
    "Session de vote terminée",
    "Votes comptabilisés"
  ];
  
  return statusLabels[status] || "Statut inconnu";
};