using Microsoft.AspNetCore.Mvc;
using System.Net.Http;
using System.Threading.Tasks;

[Route("api/[controller]")]
[ApiController]
public class RealEstateApiController : ControllerBase
{
    private readonly HttpClient _httpClient;

    public RealEstateApiController(HttpClient httpClient)
    {
        _httpClient = httpClient;
    }

    [HttpPost("download")]
    public async Task<IActionResult> DownloadWebsite([FromBody] WebsiteRequest request)
    {
        if (string.IsNullOrWhiteSpace(request.Url))
        {
            return BadRequest("URL cannot be empty.");
        }

        try
        {
            var response = await _httpClient.GetAsync(request.Url);
            response.EnsureSuccessStatusCode();
            var content = await response.Content.ReadAsStringAsync();

            // Optional: Save content to a file
            System.IO.File.WriteAllText("DownloadedWebsite.html", content);

            return Ok("Website downloaded successfully.");
        }
        catch (HttpRequestException ex)
        {
            return StatusCode(500, $"Error downloading website: {ex.Message}");
        }
    }
}

public class WebsiteRequest
{
    public string Url { get; set; }
}
