name: Daily CSV Update

on:
    schedule:
        - cron: "0 8 * * *" # Runs every day at midnight (UTC)
    workflow_dispatch:

jobs:
    update-csv:
        runs-on: ubuntu-latest

        steps:
            - name: Checkout Repository
              uses: actions/checkout@v2

            - name: Download and Compare CSV Files
              id: download-and-compare
              run: |
                  # Define a custom name mapping (URL to Filename)
                  declare -A csv_mapping
                  csv_mapping["https://portal.amfiindia.com/DownloadSchemeData_Po.aspx?mf=0"]="mf.csv" 
                  csv_mapping["https://nsearchives.nseindia.com/content/equities/EQUITY_L.csv"]="equity.csv"
                  csv_mapping["https://nsearchives.nseindia.com/content/equities/eq_etfseclist.csv"]="etf.csv"
                  csv_mapping["https://nsearchives.nseindia.com/content/equities/DEBT.csv"]="debt.csv"
                  csv_mapping["https://rbidocs.rbi.org.in/rdocs/Content/DOCs/SOVERIGNGOLDBONDS.xlsx"]="sgb.xlsx"
                  csv_mapping["https://nsearchives.nseindia.com/content/equities/List_of_Active_Securities_CM_DEBT.csv"]="active.csv"

                  # Create a directory to store the new CSV files
                  mkdir -p new-csv-files

                  # Loop through the URLs and custom names
                  for url in "${!csv_mapping[@]}"; do
                    # Get the custom filename associated with the URL
                    file_name="${csv_mapping[$url]}"

                    # Download the CSV file
                    if wget --user-agent="Mozilla/5.0" -O "new-csv-files/$file_name" "$url"; then
                      echo "Downloaded $file_name successfully."
                    else
                      echo "Download of $file_name failed."
                      continue  # Skip to the next file on failure
                    fi

                    # Compare the new CSV with the existing one
                    if ! diff -q "new-csv-files/$file_name" "data/$file_name"; then
                      echo "$file_name is different from the existing file."
                      echo "DIFFERENT_CSV=true" >> $GITHUB_ENV
                    else
                      echo "$file_name is the same as the existing file. Skipping commit."
                      echo "DIFFERENT_CSV=false" >> $GITHUB_ENV
                    fi
                  done

            - name: Overwrite CSV Files
              if: steps.download-and-compare.outputs.DIFFERENT_CSV == 'true'
              run: |
                  # Copy the new CSV files to the data directory
                  cp new-csv-files/* data/

            - name: Commit and Push Changes
              if: steps.download-and-compare.outputs.DIFFERENT_CSV == 'true'
              run: |
                  git config --global user.name "GitHub Actions"
                  git config --global user.email "actions@github.com"

                  timestamp=$(date +'%Y%m%d%H%M%S')
                  branch_name="update-csv-$timestamp"

                  git checkout -b "$branch_name"

                  git add data/*

                  git commit -m "Update CSV files"

                  git push origin "$branch_name"

                  git checkout main
                  git merge --no-ff "$branch_name"
                  git push origin main

                  # Delete the temporary branch
                  git branch -d "$branch_name"
                  git push origin --delete "$branch_name"
