using Microsoft.EntityFrameworkCore;
using detect.gui.Models.Entities;
using xwd.utils;

namespace detect.gui.Context;

public class DetectContext : DbContext
{
    private readonly string _dbPath = AppSettingsManager.Manager().GetString("Database.Path");

    public DetectContext()
    {
        // 创建数据库
        if (base.Database.EnsureCreated())
            InitData();
    }

    /// <summary>
    /// 初始化
    /// </summary>
    private void InitData()
    {
        Set<AuthorityEntity>().Add(new AuthorityEntity { Id = 1, Name = "首页", ParentId = 0 });
        Set<AuthorityEntity>().Add(new AuthorityEntity { Id = 2, Name = "任务管理", ParentId = 0 });
        Set<AuthorityEntity>().Add(new AuthorityEntity { Id = 3, Name = "设备管理", ParentId = 0 });
        Set<AuthorityEntity>().Add(new AuthorityEntity { Id = 4, Name = "日志查看", ParentId = 0 });
        Set<AuthorityEntity>().Add(new AuthorityEntity { Id = 5, Name = "用户管理", ParentId = 0 });
        
        Set<UserEntity>().Add(new UserEntity { Id = 1, Username = "admin", Password = "winner!", RealName = "管理员" });
        
        Set<UserAuthorityEntity>().Add(new UserAuthorityEntity { Id = 1, UserId = 1, AuthorityId = 1 });
        Set<UserAuthorityEntity>().Add(new UserAuthorityEntity { Id = 2, UserId = 1, AuthorityId = 2 });
        Set<UserAuthorityEntity>().Add(new UserAuthorityEntity { Id = 3, UserId = 1, AuthorityId = 3 });
        Set<UserAuthorityEntity>().Add(new UserAuthorityEntity { Id = 4, UserId = 1, AuthorityId = 4 });
        
        // Set<DeviceEntity>().Add(new DeviceEntity { Id = 1, Name = "设备11", DeviceIp = "192.168.101.253" });
        // Set<DeviceEntity>().Add(new DeviceEntity { Id = 2, Name = "设备12" });
        // Set<DeviceEntity>().Add(new DeviceEntity { Id = 3, Name = "设备13" });

        SaveChanges();
    }
    
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlite($"Data Source={_dbPath}detect.db");
        base.OnConfiguring(optionsBuilder);
    }
    
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<UserEntity>().ToTable("dat_user");
        modelBuilder.Entity<AuthorityEntity>().ToTable("dat_authority");
        modelBuilder.Entity<UserAuthorityEntity>().ToTable("dat_user_authority");
        modelBuilder.Entity<DetectTaskEntity>().ToTable("dat_detect_task");
        modelBuilder.Entity<DetectTaskLogEntity>().ToTable("dat_detect_task_log");
        modelBuilder.Entity<DetectTaskProgressEntity>().ToTable("dat_detect_task_progress");
        modelBuilder.Entity<DeviceEntity>().ToTable("dat_device");
        modelBuilder.Entity<LogEntity>().ToTable("dat_log");

        modelBuilder.Entity<UserEntity>().Property(p => p.CreateTime).HasDefaultValueSql("datetime('now')");
        modelBuilder.Entity<UserEntity>().Property(p => p.UpdateTime).HasDefaultValueSql("datetime('now')");
        modelBuilder.Entity<AuthorityEntity>().Property(p => p.CreateTime).HasDefaultValueSql("datetime('now')");
        modelBuilder.Entity<AuthorityEntity>().Property(p => p.UpdateTime).HasDefaultValueSql("datetime('now')");
        modelBuilder.Entity<AuthorityEntity>().Property(p => p.ParentId).HasDefaultValueSql("0");
        modelBuilder.Entity<DetectTaskEntity>().Property(p => p.CreateTime).HasDefaultValueSql("datetime('now')");
        modelBuilder.Entity<DetectTaskEntity>().Property(p => p.UpdateTime).HasDefaultValueSql("datetime('now')");
        modelBuilder.Entity<DetectTaskLogEntity>().Property(p => p.CreateTime).HasDefaultValueSql("datetime('now')");
        modelBuilder.Entity<DetectTaskProgressEntity>().Property(p => p.CreateTime).HasDefaultValueSql("datetime('now')");
        modelBuilder.Entity<DeviceEntity>().Property(p => p.CreateTime).HasDefaultValueSql("datetime('now')");
        modelBuilder.Entity<DeviceEntity>().Property(p => p.UpdateTime).HasDefaultValueSql("datetime('now')");
        modelBuilder.Entity<LogEntity>().Property(p => p.CreateTime).HasDefaultValueSql("datetime('now')");
        base.OnModelCreating(modelBuilder);
    }
}