# Prompt the user for a choice between prod and preprod
$environment = Read-Host "Enter environment (prod or preprod, testa)"

# Set the paths to search for log files based on the user's choice
if ($environment -eq "prod") {
    $Paths = "\\REJ-P-WEB001.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-WEB002.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-WEB003.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-WEB004.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-WEB005.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-WEB006.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-WEB007.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-WEB008.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-APP001.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-APP002.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-APP003.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-APP004.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-API001.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-API002.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-TCS001.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-TCS002.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-TCS003.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-TCS004.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-TCS005.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-TCS006.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-TCS007.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-TCS008.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-TCS009.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-TCS010.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-TCS011.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-TCS012.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-TCS013.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-TCS014.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-TCS015.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-TCS016.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-TCF001.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-TCF002.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-MDL001.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-MDL002.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-SPS001.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-SPS002.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-SPS003.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-SPS004.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-SPS005.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-SPS006.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-SPS007.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-SPS008.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-SPS009.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-SPS010.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-SPS011.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-DPS001.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-DPS002.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-DPS003.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-DPS004.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-DPS005.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-DPS006.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-FAS001.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-FAS002.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-FAS003.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-BCM001.REJprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-P-BCM002.REJprod.local\d-drive\IFSBOSystem\Log"
} elseif ($environment -eq "preprod") {
    $Paths =  "\\REJ-Q-WEB101.REJpreprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-Q-WEB102.REJpreprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-Q-WEB103.REJpreprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-Q-WEB104.REJpreprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-Q-WEB105.REJpreprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-Q-WEB106.REJpreprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-Q-APP101.REJpreprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-Q-APP102.REJpreprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-Q-API101.REJpreprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-Q-API102.REJpreprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-Q-TCS101.REJpreprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-Q-TCS102.REJpreprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-Q-TCF101.REJpreprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-Q-TCF102.REJpreprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-Q-MDL101.REJpreprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-Q-MDL102.REJpreprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-Q-SPS101.REJpreprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-Q-SPS102.REJpreprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-Q-DPS101.REJpreprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-Q-DPS102.REJpreprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-Q-FAS101.REJpreprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-Q-FAS102.REJpreprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-Q-BCM101.REJpreprod.local\d-drive\IFSBOSystem\Log",
              "\\REJ-Q-BCM102.REJpreprod.local\d-drive\IFSBOSystem\Log"
}elseif ($environment -eq "testa") {
    $Paths =  "\\rej-a-api201\D-Drive\IFSBOSystem\Log",
              "\\rej-a-app201\D-Drive\IFSBOSystem\Log",
              "\\rej-a-bcm201\D-Drive\IFSBOSystem\Log",
              "\\rej-a-dps201\D-Drive\IFSBOSystem\Log",
              "\\rej-a-fas201\D-Drive\IFSBOSystem\Log",
              "\\rej-a-mdl201\D-Drive\IFSBOSystem\Log",
              "\\rej-a-sps201\D-Drive\IFSBOSystem\Log",
              "\\rej-a-tcf201\D-Drive\IFSBOSystem\Log",
              "\\rej-a-tcs201\D-Drive\IFSBOSystem\Log",
              "\\rej-a-web201\D-Drive\IFSBOSystem\Log"

} else {
    Write-Host "Invalid environment entered. Please enter correct envioment."
    exit
}

        

# Define the script block to run in each thread
$ScriptBlock = {
    param ($Path)

    # Find all log files in the directory
$logFiles = Get-ChildItem -Path $Path -Filter "*.log" -File -Recurse | Where-Object { $_.PSIsContainer -or $_.Name -notmatch '^\d{4}-\d{2}-\d{2}' }


    if ($logFiles.Count -eq 0) {
        Write-Warning "No log files found in $($Path)"
        return
    }

    # Get the current date and format it as "yyyy-MM-dd"
    $currentDate = Get-Date -Format "yyyy-MM-dd"

    # Iterate through each log file and count the number of lines written to the file during the current day
    $logFileCounts = foreach ($file in $logFiles) {
        if ($file.LastWriteTime -gt (Get-Date).AddDays(-1)) {
            # Get the contents of the file and count the number of lines written to the file during the current day
            $count = (Get-Content $file.FullName | Where-Object { $_ -match $currentDate }).Count

            # Output the file name and the count of lines written to the file during the current day
            [PSCustomObject]@{
                FileName = $file.Name
                FilePath = Split-Path $file.FullName -Parent
                LinesWrittenToday = $count
            }
        }
    }

    # Return the log file counts for this path
    return $logFileCounts
}

# Define an array to hold the job results
$jobResults = @()

# Start a job for each path
foreach ($Path in $Paths) {
    $job = Start-Job -ScriptBlock $ScriptBlock -ArgumentList $Path
    $jobResults += $job
}

# Wait for all jobs to complete
while (@($jobResults | Where-Object { $_.State -eq 'Running' }).Count -gt 0) {
    Start-Sleep -Milliseconds 110
}

# Get the results of each job and combine them into a single array
$logFileCounts = foreach ($job in $jobResults) {
    $job | Receive-Job
}

# Group the log file counts by file name and path and sum the line counts
$groupedLogFileCounts = $logFileCounts | ForEach-Object {
    [PSCustomObject]@{
        FileName = $_.FileName
        FilePath = $_.FilePath
        LinesWrittenToday = $_.LinesWrittenToday
    }
} | Group-Object -Property @{Expression = {$_.FileName + "|" + $_.FilePath}} | ForEach-Object {
    $fileName, $filePath = $_.Name.Split("|")
    $serverName = $filePath -replace ".*\\\\([^\\]+).*", '$1'
    [PSCustomObject]@{
        FileName = $fileName
        ServerName = $serverName
        TotalLinesWrittenToday = ($_.Group | Measure-Object -Property LinesWrittenToday -Sum).Sum
    }
} | Where-Object { $_.TotalLinesWrittenToday -ne 0 }

# Output the grouped log file counts
$groupedLogFileCounts | Sort-Object -Property ServerName | Format-Table -GroupBy ServerName -Property FileName, TotalLinesWrittenToday -AutoSize

# Calculate and output the total lines written today for all files
$totalLinesWrittenToday = $groupedLogFileCounts | Measure-Object -Property TotalLinesWrittenToday -Sum
"Total lines written today: $($totalLinesWrittenToday.Sum)"


$groupedcount = $groupedLogFileCounts | Group-Object -Property { $_.ServerName.Replace(".REJpreprod.local", "") } | Select-Object Name, @{Name="Types of Log Files"; Expression={$_.Count}}

$groupedcount