import fs from 'fs';
import path from 'path';

function fixExistingData() {
  const dataDir = path.resolve('public/data');
  if (!fs.existsSync(dataDir)) {
    console.error('Data directory does not exist!');
    return;
  }

  const files = fs.readdirSync(dataDir).filter(f => f.endsWith('.json'));
  console.log(`Found ${files.length} data files to clean up...`);

  files.forEach(file => {
    const filePath = path.join(dataDir, file);
    try {
      const content = fs.readFileSync(filePath, 'utf-8');
      const data = JSON.parse(content);

      if (!Array.isArray(data)) return;

      let lastKnownInflow: number | null = null;
      let lastKnownVolume: number | null = null;
      let fixCountInflow = 0;
      let fixCountVolume = 0;

      // First pass (oldest to newest): find and propagate values forward
      data.forEach(record => {
        // Handle inflow
        if (record.inflow !== undefined && record.inflow !== null && record.inflow !== 0) {
          lastKnownInflow = record.inflow;
        } else if ((record.inflow === undefined || record.inflow === null || record.inflow === 0) && lastKnownInflow !== null) {
          record.inflow = lastKnownInflow;
          fixCountInflow++;
        }

        // Handle volume
        if (record.volume !== undefined && record.volume !== null && record.volume !== 0) {
          lastKnownVolume = record.volume;
        } else if ((record.volume === undefined || record.volume === null || record.volume === 0) && lastKnownVolume !== null) {
          record.volume = lastKnownVolume;
          fixCountVolume++;
        }
      });

      // Second pass (newest to oldest): if there were zeros at the very beginning of the file
      // before any non-zero value was found, propagate backwards.
      let nextKnownInflow: number | null = null;
      let nextKnownVolume: number | null = null;
      for (let i = data.length - 1; i >= 0; i--) {
        const record = data[i];
        if (record.inflow !== undefined && record.inflow !== null && record.inflow !== 0) {
          nextKnownInflow = record.inflow;
        } else if ((record.inflow === undefined || record.inflow === null || record.inflow === 0) && nextKnownInflow !== null) {
          record.inflow = nextKnownInflow;
          fixCountInflow++;
        }

        if (record.volume !== undefined && record.volume !== null && record.volume !== 0) {
          nextKnownVolume = record.volume;
        } else if ((record.volume === undefined || record.volume === null || record.volume === 0) && nextKnownVolume !== null) {
          record.volume = nextKnownVolume;
          fixCountVolume++;
        }
      }

      if (fixCountInflow > 0 || fixCountVolume > 0) {
        fs.writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf-8');
        console.log(`[${file}] Cleaned up ${fixCountInflow} inflows and ${fixCountVolume} volumes.`);
      }
    } catch (e: any) {
      console.error(`Error processing file ${file}:`, e.message);
    }
  });

  console.log('Cleanup finished.');
}

fixExistingData();
