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 = "0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512";

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";
};