var builder = WebApplication.CreateBuilder(args);
builder.Services.AddCors(options =>{ 
options.AddPolicy("AllowAllOrigins", policy =>{
policy.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod();});
});

var app = builder.Build();
app.UseCors("AllowAllOrigins"); 
app.UseStaticFiles(); 
var baseDir = Directory.GetCurrentDirectory();
Directory.CreateDirectory(baseDir);
var root = Path.Combine(baseDir, "wwwroot");
var html = Path.Combine(root, "html");

app.MapGet("/", async () =>
{
var filePath = Path.Combine(root, "home.html");
if (!File.Exists(filePath))
return Results.NotFound("File home.html tidak ditemukan.");
var content = await File.ReadAllTextAsync(filePath);
return Results.Text(content, "text/html", Encoding.UTF8);
});

app.MapGet("/api/root", () =>
{
var files = Directory.GetFiles(root)
.Select(f => Path.GetFileName(f))
.ToArray();
return Results.Json(files);
});

app.MapGet("/api/list", () =>
{
var html = Path.Combine(root, "html");
var files = Directory.GetFiles(html)
.Select(f => Path.GetFileName(f))
.ToArray();
return Results.Json(files);
});

app.MapPost("/api/simpan/{nama}", async (HttpContext context, string nama) =>
{
try
{
using var reader = new StreamReader(context.Request.Body, Encoding.UTF8);
var isi = await reader.ReadToEndAsync();
var path = Path.Combine(html, nama);
await File.WriteAllTextAsync(path, isi);
return Results.Ok($"Tersimpan ke {nama}");
}
catch (Exception ex)
{
return Results.Problem($"Gagal simpan: {ex.Message}");
}
});

app.MapGet("/api/baca/{nama}", async (string nama) =>
{
var path = Path.Combine(html, nama);
if (!File.Exists(path)) return Results.NotFound("File tidak ditemukan.");
var isi = await File.ReadAllTextAsync(path);
return Results.Text(isi, "text/html", Encoding.UTF8);
});

app.MapPost("/api/rename", async (HttpContext context) =>
{
var form = await context.Request.ReadFormAsync();
var dari = form["from"].ToString();
var ke = form["to"].ToString();
if (string.IsNullOrWhiteSpace(dari) || string.IsNullOrWhiteSpace(ke))
return Results.BadRequest("Nama file asal dan tujuan wajib diisi.");
var pathFrom = Path.Combine(html, dari);
var pathTo = Path.Combine(html, ke);
if (!File.Exists(pathFrom)) return Results.NotFound("File asal tidak ditemukan.");
if (File.Exists(pathTo)) return Results.BadRequest("File tujuan sudah ada.");
File.Move(pathFrom, pathTo);
return Results.Ok($"File {dari} diubah jadi {ke}");
});

app.MapDelete("/api/hapus/{nama}", (string nama) =>
{
var path = Path.Combine(html, nama);
if (!File.Exists(path)) return Results.NotFound("File tidak ditemukan.");
File.Delete(path);
return Results.Ok($"File {nama} dihapus.");
});

app.MapGet("/api/fetch", async (HttpContext context) =>
{
var query = context.Request.Query;
var fn = query["fn"].ToString();
var urlt = query["urlt"].ToString();
if (string.IsNullOrWhiteSpace(fn) || string.IsNullOrWhiteSpace(urlt))
return Results.BadRequest("Missing fn, or url");
using var httpClient = new HttpClient();
var content = await httpClient.GetStringAsync(urlt);
var filePath = Path.Combine(html, fn);
await File.WriteAllTextAsync(filePath, content);
return Results.Ok($"Saved to {filePath}");
});

app.MapPost("/api/upload", async (HttpRequest request) =>
{
var form = await request.ReadFormAsync();
var file = form.Files["file"];
if (file == null || file.Length == 0)
return Results.BadRequest("No file uploaded.");
var fileName = Path.GetFileName(file.FileName);
var savePath = Path.Combine(html, fileName);
Directory.CreateDirectory(html); 
using var stream = new FileStream(savePath, FileMode.Create);
await file.CopyToAsync(stream);
return Results.Ok($"File saved to /html/{fileName}");
});
app.Run();
