import { ethers } from 'hardhat';
import * as fs from 'fs';
import * as path from 'path';

async function main() {
  console.log('Starting deployment to Besu network...\n');

  const [deployer] = await ethers.getSigners();
  console.log('Deploying contracts with account:', deployer.address);

  const balance = await ethers.provider.getBalance(deployer.address);
  console.log('Account balance:', ethers.formatEther(balance), 'ETH\n');

  // Deploy LicenseNFT
  console.log('Deploying LicenseNFT...');
  const LicenseNFT = await ethers.getContractFactory('LicenseNFT');
  const licenseNFT = await LicenseNFT.deploy();
  await licenseNFT.waitForDeployment();
  const licenseNFTAddress = await licenseNFT.getAddress();
  console.log('LicenseNFT deployed to:', licenseNFTAddress);

  // Deploy ApprovalManager
  console.log('\nDeploying ApprovalManager...');
  const ApprovalManager = await ethers.getContractFactory('ApprovalManager');
  const approvalManager = await ApprovalManager.deploy();
  await approvalManager.waitForDeployment();
  const approvalManagerAddress = await approvalManager.getAddress();
  console.log('ApprovalManager deployed to:', approvalManagerAddress);

  // Deploy DocumentChain
  console.log('\nDeploying DocumentChain...');
  const DocumentChain = await ethers.getContractFactory('DocumentChain');
  const documentChain = await DocumentChain.deploy();
  await documentChain.waitForDeployment();
  const documentChainAddress = await documentChain.getAddress();
  console.log('DocumentChain deployed to:', documentChainAddress);

  // Deploy WorkflowRegistry
  console.log('\nDeploying WorkflowRegistry...');
  const WorkflowRegistry = await ethers.getContractFactory('WorkflowRegistry');
  const workflowRegistry = await WorkflowRegistry.deploy();
  await workflowRegistry.waitForDeployment();
  const workflowRegistryAddress = await workflowRegistry.getAddress();
  console.log('WorkflowRegistry deployed to:', workflowRegistryAddress);

  // Summary
  console.log('\n========================================');
  console.log('Deployment Complete!');
  console.log('========================================');
  console.log('Contract Addresses:');
  console.log('  LicenseNFT:', licenseNFTAddress);
  console.log('  ApprovalManager:', approvalManagerAddress);
  console.log('  DocumentChain:', documentChainAddress);
  console.log('  WorkflowRegistry:', workflowRegistryAddress);
  console.log('========================================\n');

  // Save deployment info
  const deploymentInfo = {
    network: 'besu',
    chainId: 1337,
    deployer: deployer.address,
    timestamp: new Date().toISOString(),
    contracts: {
      LicenseNFT: licenseNFTAddress,
      ApprovalManager: approvalManagerAddress,
      DocumentChain: documentChainAddress,
      WorkflowRegistry: workflowRegistryAddress,
    },
  };

  const deploymentPath = path.join(__dirname, '../deployments');
  if (!fs.existsSync(deploymentPath)) {
    fs.mkdirSync(deploymentPath, { recursive: true });
  }

  fs.writeFileSync(
    path.join(deploymentPath, 'deployment.json'),
    JSON.stringify(deploymentInfo, null, 2)
  );
  console.log('Deployment info saved to deployments/deployment.json');

  // Generate .env updates
  console.log('\n========================================');
  console.log('Add these to your backend/.env file:');
  console.log('========================================');
  console.log(`CONTRACT_ADDRESS_LICENSE_NFT=${licenseNFTAddress}`);
  console.log(`CONTRACT_ADDRESS_APPROVAL_MANAGER=${approvalManagerAddress}`);
  console.log(`CONTRACT_ADDRESS_DOCUMENT_CHAIN=${documentChainAddress}`);
  console.log(`CONTRACT_ADDRESS_WORKFLOW_REGISTRY=${workflowRegistryAddress}`);
  console.log('========================================\n');

  return deploymentInfo;
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });
