// Copyright 2017-2026 @pezkuwi/app-council authors & contributors
// SPDX-License-Identifier: Apache-2.0

import type { DeriveCouncilVotes, DeriveElectionsInfo } from '@pezkuwi/api-derive/types';
import type { AccountId } from '@pezkuwi/types/interfaces';

import React from 'react';

import { Button } from '@pezkuwi/react-components';
import { useApi, useBestNumber, useCall } from '@pezkuwi/react-hooks';

import { useModuleElections } from '../useModuleElections.js';
import Candidates from './Candidates.js';
import Members from './Members.js';
import SubmitCandidacy from './SubmitCandidacy.js';
import Summary from './Summary.js';
import Vote from './Vote.js';

interface Props {
  className?: string;
  prime?: AccountId | null;
}

const transformVotes = {
  transform: (entries: DeriveCouncilVotes): Record<string, AccountId[]> =>
    entries.reduce<Record<string, AccountId[]>>((result, [voter, { votes }]) => {
      votes.forEach((candidate): void => {
        const address = candidate.toString();

        if (!result[address]) {
          result[address] = [];
        }

        result[address].push(voter);
      });

      return result;
    }, {})
};

function Overview ({ className = '', prime }: Props): React.ReactElement<Props> {
  const { api } = useApi();
  const bestNumber = useBestNumber();
  const electionsInfo = useCall<DeriveElectionsInfo>(api.derive.elections.info);
  const allVotes = useCall<Record<string, AccountId[]>>(api.derive.council.votes, undefined, transformVotes);
  const modElections = useModuleElections();
  const hasElections = !!modElections;

  return (
    <div className={className}>
      <Summary
        bestNumber={bestNumber}
        electionsInfo={electionsInfo}
        hasElections={!!modElections}
      />
      {hasElections && (
        <Button.Group>
          <Vote electionsInfo={electionsInfo} />
          <SubmitCandidacy electionsInfo={electionsInfo} />
        </Button.Group>
      )}
      <Members
        allVotes={allVotes}
        electionsInfo={electionsInfo}
        hasElections={hasElections}
        prime={prime}
      />
      {hasElections && (
        <Candidates
          allVotes={allVotes}
          electionsInfo={electionsInfo}
          hasElections={hasElections}
        />
      )}
    </div>
  );
}

export default React.memo(Overview);
