const fs = require('fs');
const path = require('path');
const core = require('@actions/core');
const fetch = require('node-fetch');

(async () => {
  try {
    const token = process.env.GITEA_TOKEN;
    const uploadUrl = core.getInput('upload_url');
    const assetPath = core.getInput('asset_path');
    const assetName = core.getInput('asset_name');
    const contentType = core.getInput('asset_content_type');

    const fileData = fs.readFileSync(assetPath);

    const url = `${uploadUrl}?name=${encodeURIComponent(assetName)}`;

    const res = await fetch(url, {
      method: 'POST',
      headers: {
        Authorization: `token ${token}`,
        'Content-Type': contentType,
      },
      body: fileData
    });

    if (!res.ok) {
      throw new Error(`Failed to upload asset: ${res.status} ${await res.text()}`);
    }

    core.info(`Asset uploaded: ${assetName}`);
  } catch (err) {
    core.setFailed(err.message);
  }
})();
