import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Site } from './site.entity';
import { UpdateSiteSettingsDto } from './site-settings.dto';
import { DEFAULT_SITE_CONFIG, SYSTEM_CONSTANTS } from '../../../config/common/constants';

@Injectable()
export class SiteSettingsService {
  constructor(
    @InjectRepository(Site)
    private readonly siteRepository: Repository<Site>,
  ) {}

  /**
   * 获取站点设置
   */
  async getSiteSettings() {
    // 获取默认站点（id = 1）
    const site = await this.siteRepository.findOne({
      where: { site_id: SYSTEM_CONSTANTS.DEFAULT_SITE_ID },
    });

    if (!site) {
      // 如果没有找到站点，返回默认值
      return { ...DEFAULT_SITE_CONFIG };
    }

    return {
      site_name: site.site_name || DEFAULT_SITE_CONFIG.site_name,
      site_title: site.site_title || DEFAULT_SITE_CONFIG.site_title,
      site_keywords: site.site_keywords || DEFAULT_SITE_CONFIG.site_keywords,
      site_description: site.site_description || DEFAULT_SITE_CONFIG.site_description,
      site_logo: site.site_logo || DEFAULT_SITE_CONFIG.site_logo,
      site_favicon: site.site_favicon || DEFAULT_SITE_CONFIG.site_favicon,
      icp_number: site.icp_number || DEFAULT_SITE_CONFIG.icp_number,
      copyright: site.copyright || DEFAULT_SITE_CONFIG.copyright,
      site_status: site.site_status || DEFAULT_SITE_CONFIG.site_status,
      close_reason: site.close_reason || DEFAULT_SITE_CONFIG.close_reason,
    };
  }

  /**
   * 更新站点设置
   */
  async updateSiteSettings(updateSiteSettingsDto: UpdateSiteSettingsDto) {
    const {
      site_name,
      site_title,
      site_keywords,
      site_description,
      site_logo,
      site_favicon,
      icp_number,
      copyright,
      site_status,
      close_reason,
    } = updateSiteSettingsDto;

    // 查找或创建默认站点
    let site = await this.siteRepository.findOne({
      where: { id: SYSTEM_CONSTANTS.DEFAULT_SITE_ID },
    });

    if (!site) {
      // 创建默认站点
      site = this.siteRepository.create({
        id: SYSTEM_CONSTANTS.DEFAULT_SITE_ID,
        site_name: site_name || DEFAULT_SITE_CONFIG.site_name,
        site_title: site_title || DEFAULT_SITE_CONFIG.site_title,
        site_keywords: site_keywords || DEFAULT_SITE_CONFIG.site_keywords,
        site_description: site_description || DEFAULT_SITE_CONFIG.site_description,
        site_logo: site_logo || DEFAULT_SITE_CONFIG.site_logo,
        site_favicon: site_favicon || DEFAULT_SITE_CONFIG.site_favicon,
        icp_number: icp_number || DEFAULT_SITE_CONFIG.icp_number,
        copyright: copyright || DEFAULT_SITE_CONFIG.copyright,
        site_status: site_status || DEFAULT_SITE_CONFIG.site_status,
        close_reason: close_reason || DEFAULT_SITE_CONFIG.close_reason,
      });
    } else {
      // 更新现有站点
      if (site_name !== undefined) site.site_name = site_name;
      if (site_title !== undefined) site.site_title = site_title;
      if (site_keywords !== undefined) site.site_keywords = site_keywords;
      if (site_description !== undefined) site.site_description = site_description;
      if (site_logo !== undefined) site.site_logo = site_logo;
      if (site_favicon !== undefined) site.site_favicon = site_favicon;
      if (icp_number !== undefined) site.icp_number = icp_number;
      if (copyright !== undefined) site.copyright = copyright;
      if (site_status !== undefined) site.site_status = site_status;
      if (close_reason !== undefined) site.close_reason = close_reason;
    }

    await this.siteRepository.save(site);
    return { message: '站点设置更新成功' };
  }

  /**
   * 重置站点设置为默认值
   */
  async resetSiteSettings() {
    // 删除现有站点配置
    await this.siteRepository.delete({ id: SYSTEM_CONSTANTS.DEFAULT_SITE_ID });

    // 创建默认站点配置
    const defaultSite = this.siteRepository.create({
      id: SYSTEM_CONSTANTS.DEFAULT_SITE_ID,
      ...DEFAULT_SITE_CONFIG,
    });

    await this.siteRepository.save(defaultSite);
    return { message: '站点设置重置成功' };
  }
}