using JobsMedical.Web.Data;
using JobsMedical.Web.Models;
using Microsoft.EntityFrameworkCore;

namespace JobsMedical.Web.Services.Scraping;

/// <summary>Loads/creates the single platform-settings row (Id=1).</summary>
public class SettingsService
{
    private readonly AppDbContext _db;
    public SettingsService(AppDbContext db) => _db = db;

    public async Task<AppSetting> GetAsync()
    {
        var s = await _db.AppSettings.FirstOrDefaultAsync(x => x.Id == 1);
        if (s is null)
        {
            s = new AppSetting { Id = 1 };
            _db.AppSettings.Add(s);
            await _db.SaveChangesAsync();
        }
        return s;
    }

    public async Task SaveAsync(AppSetting incoming)
    {
        var s = await GetAsync();
        s.Mode = incoming.Mode;
        s.AutoPublishMinConfidence = Math.Clamp(incoming.AutoPublishMinConfidence, 0, 100);
        s.AiEnabled = incoming.AiEnabled;
        s.AiEndpoint = incoming.AiEndpoint?.Trim();
        s.AiApiKey = incoming.AiApiKey?.Trim();
        s.AiModel = incoming.AiModel?.Trim();
        s.AiSystemPrompt = AppSetting.DefaultPrompt;   // hardcoded & read-only — keep the column in sync
        s.AiAutoApprove = incoming.AiAutoApprove;
        s.AiUseProxy = incoming.AiUseProxy;
        // Channel scraping sources
        s.AutoIngestEnabled = incoming.AutoIngestEnabled;
        s.IngestIntervalMinutes = Math.Max(1, incoming.IngestIntervalMinutes);
        s.TelegramEnabled = incoming.TelegramEnabled;
        s.TelegramChannels = incoming.TelegramChannels?.Trim();
        s.BaleEnabled = incoming.BaleEnabled;
        s.BaleBotToken = incoming.BaleBotToken?.Trim();
        s.DemoMode = incoming.DemoMode;
        s.WebsitesEnabled = incoming.WebsitesEnabled;
        s.WebsiteUrls = incoming.WebsiteUrls?.Trim();
        s.IngestProxyUrl = incoming.IngestProxyUrl?.Trim();
        s.TelegramUseProxy = incoming.TelegramUseProxy;
        s.BaleUseProxy = incoming.BaleUseProxy;
        s.DivarUseProxy = incoming.DivarUseProxy;
        s.MedjobsUseProxy = incoming.MedjobsUseProxy;
        s.WebsitesUseProxy = incoming.WebsitesUseProxy;
        s.DivarEnabled = incoming.DivarEnabled;
        s.DivarCity = string.IsNullOrWhiteSpace(incoming.DivarCity) ? "tehran" : incoming.DivarCity.Trim();
        s.DivarQueries = incoming.DivarQueries?.Trim();
        s.MedjobsEnabled = incoming.MedjobsEnabled;
        s.MedjobsMaxAds = Math.Clamp(incoming.MedjobsMaxAds, 1, 500);
        s.IranEstekhdamEnabled = incoming.IranEstekhdamEnabled;
        s.IranEstekhdamMaxAds = Math.Clamp(incoming.IranEstekhdamMaxAds, 1, 500);
        s.IranEstekhdamUseProxy = incoming.IranEstekhdamUseProxy;
        s.MedboomEnabled = incoming.MedboomEnabled;
        s.MedboomMaxAds = Math.Clamp(incoming.MedboomMaxAds, 1, 500);
        s.MedboomUseProxy = incoming.MedboomUseProxy;
        s.SmsEnabled = incoming.SmsEnabled;
        s.SmsApiKey = incoming.SmsApiKey?.Trim();
        s.SmsTemplate = incoming.SmsTemplate?.Trim();
        s.SmsSender = incoming.SmsSender?.Trim();
        s.NeshanMapKey = incoming.NeshanMapKey?.Trim();
        s.WebNotificationsEnabled = incoming.WebNotificationsEnabled;
        s.PushEnabled = incoming.PushEnabled;
        s.VapidPublicKey = incoming.VapidPublicKey?.Trim();
        s.VapidPrivateKey = incoming.VapidPrivateKey?.Trim();
        s.VapidSubject = string.IsNullOrWhiteSpace(incoming.VapidSubject) ? "mailto:admin@hamkadr.ir" : incoming.VapidSubject.Trim();
        s.UpdatedAt = DateTime.UtcNow;
        await _db.SaveChangesAsync();
    }
}
