#Scratch
# API TIME
$apiEndpoint = "https://minecraft.curseforge.com"
$mod_id = '255109'
$Project_id = '255109'
$apiKey = '$2a$10$JImTmfu6axMXbi7vPRNZWesM86H83rda4Cs1D7MElzm24XaXBSqYK'

$FileContent = [IO.File]::ReadAllBytes('C:\Users\Jzjad\Documents\PiTek\Curse\PiTek 3.0-3.8.2.zip');


# $file = "C:\Users\Jzjad\Documents\PiTek\Curse\PiTek 3.0-3.8.2.zip"

# $file = get-content "C:\Users\Jzjad\Documents\PiTek\Curse\PiTek 3.0-3.8.2.zip" -Enc Byte -raw
# Python example from previous project
# def get_mod_json():
#   apiRequest = requests.get(f"{apiEndpoint}/mods/{mod_id}/files", params={
#     'modLoaderType': 'forge',
#     'pageSize': '1'
#   }, headers = headers)
#   return apiRequest.json()

#Example?
#Invoke-RestMethod -Method POST -Uri $uri -Header $header -Body ($body | ConvertTo-Json) -ContentType 'application/json'

#$GameInfo = Invoke-WebRequest -Uri "$apiEndpoint/mods/$mod_id/files" -Method Get -UseBasicParsing -ContentType 'application/json'

#Better example
# $GetRequest = @{
#   Uri         = "$apiEndpoint/mods/$mod_id/files"
#   Headers     = @{
#     'x-api-key' = $apiKey
#     'Accept' = 'application/json'}
#   Method      = 'Get'
#   #Body = '{}'
#   ContentType = 'application/json'
# }
# $response = Invoke-RestMethod @GetRequest

#Write-Output $response

#Upload/Post Example
# {
#   changelog: "A string describing changes.", // Can be HTML or markdown if changelogType is set.
#   changelogType: ["text", "html", "markdown"], // Optional: defaults to text
#   displayName: "Foo", // Optional: A friendly display name used on the site if provided.
#   parentFileID: 42, // Optional: The parent file of this file.
#   gameVersions: [157, 158], // A list of supported game versions, see the Game Versions API for details. Not supported if parentFileID is provided.
#   releaseType: "alpha", // One of "alpha", "beta", "release".
#   relations: {
#       projects: [{
#           slug: "mantle", // Slug of related plugin.
#           type: ["embeddedLibrary", "incompatible", "optionalDependency", "requiredDependency", "tool"] // Choose one
#       }]
#   } // Optional: An array of project relations by slug and type of dependency for inclusion in your project.
# }
$body = @{
  metadata = {
    changelog: "If this up for review then its working, and can be removed/denied for now. Thanks!",
    changelogType: ["text"],
    displayName: "PiTek-Automation-Test",
    releaseType: "alpha"
  }
  uploadFile = {$FileContent}
}

$Headers     = @{
  'X-Api-Token' = $apiKey
  'Accept' = 'application/json'
}

# Invoke-RestMethod -Uri "$apiEndpoint/api/projects/$Project_id/upload-file" -ContentType 'multipart/form-data' -Headers $Headers -Method Post -Body $body -InFile $file




# $FilePath = 'c:\temp\temp.txt';

# $fileBytes = [System.IO.File]::ReadAllBytes($FilePath);
# $fileEnc = [System.Text.Encoding]::GetEncoding('UTF-8').GetString($fileBytes);
# $boundary = [System.Guid]::NewGuid().ToString(); 
# $LF = "`r`n";

# $bodyLines = ( 
#     "--$boundary",
#     "Content-Disposition: form-data; name=`"file`"; filename=`"temp.txt`"",
#     "Content-Type: application/octet-stream$LF",
#     $fileEnc,
#     "--$boundary--$LF" 
# ) -join $LF

# Invoke-RestMethod -Uri $URL -Method Post -ContentType "multipart/form-data; boundary=`"$boundary`"" -Body $bodyLines

# $form = @{
#   metadata = {
#     changelog: "If this up for review then its working, and can be removed/denied for now. Thanks!",
#     changelogType: ["text"],
#     displayName: "PiTek-Automation-Test",
#     releaseType: "alpha"
#   }
# }
# $File = Get-Item -Path "C:\Users\Jzjad\Documents\PiTek\Curse\PiTek 3.0-3.8.2.zip"
# Invoke-WebRequest -Uri "$apiEndpoint/api/projects/$Project_id/upload-file" -Method POST -Headers $headers -Body $form -InFile $File


# $Metadata = @{
#     changelog: "If this up for review then its working, and can be removed/denied for now. Thanks!",
#     changelogType: "text",
#     displayName: "PiTek-Automation-Test",
#     releaseType: "alpha"
# }

# $Metadata = $Metadata | ConvertTo-Json

# $Fields = @{
#   data = {
#     metadata = {
#       'changelog' = "If this up for review then its working, and can be removed/denied for now. Thanks!",
#       'changelogType'= "text",
#       'displayName'= "PiTek-Automation-Test",
#       'releaseType'= "alpha"
#     }
#   }
#   file = {$FileContent}
# }
Invoke-RestMethod -Uri "$apiEndpoint/api/projects/$Project_id/upload-file" -ContentType 'multipart/form-data' -Method Post -Headers $headers -Body $Fields

