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

(async () => {
  try {
    const token = process.env.GITEA_TOKEN;
    const tag = core.getInput('tag_name');
    const name = core.getInput('release_name');
    const draft = core.getInput('draft') === 'true';
    const prerelease = core.getInput('prerelease') === 'true';
    const repo = process.env.GITEA_REPOSITORY;
    const apiUrl = `${process.env.GITEA_SERVER_URL}/api/v1/repos/${repo}/releases`;

    const res = await fetch(apiUrl, {
      method: 'POST',
      headers: {
        Authorization: `token ${token}`,
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        tag_name: tag,
        name,
        draft,
        prerelease
      })
    });

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

    const json = await res.json();
    core.setOutput('upload_url', json.upload_url);
  } catch (err) {
    core.setFailed(err.message);
  }
})();
