using Furion.DatabaseAccessor;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace YBEE.EQM.Core;

/// <summary>
/// 监测年级
/// </summary>
[Comment("监测年级")]
public class ExamGrade : DEntityBase, IEntityTypeBuilder<ExamGrade>
{
    /// <summary>
    /// 监测计划ID
    /// </summary>
    [Comment("监测计划ID")]
    [Required]
    public int ExamPlanId { get; set; }
    /// <summary>
    /// 年级ID
    /// </summary>
    [Comment("年级ID")]
    [Required]
    public short GradeId { get; set; }
    /// <summary>
    /// 年份（级）
    /// </summary>
    [Comment("年份（级）")]
    [Required]
    public short GradeBeginYear { get; set; }
    /// <summary>
    /// 年份（届）
    /// </summary>
    [Comment("年份（届）")]
    [Required]
    public short GradeEndYear { get; set; }
    /// <summary>
    /// 学段
    /// </summary>
    [Comment("学段")]
    [Required, Column(TypeName = "smallint")]
    public EducationStage EducationStage { get; set; }
    /// <summary>
    /// 学制
    /// </summary>
    [Comment("学制")]
    [Required]
    public short EducationYears { get; set; }

    /// <summary>
    /// 是否需要自编监测号
    /// </summary>
    [Comment("是否需要自编监测号")]
    [Required]
    public bool IsRequiredSelfExamNumber { get; set; } = false;
    /// <summary>
    /// 自编监测号长度
    /// </summary>
    [Comment("自编监测号长度")]
    [Required]
    public short SelfExamNumberLength { get; set; } = 0;

    /// <summary>
    /// 是否需要抽样监测
    /// </summary>
    [Comment("是否需要抽样监测")]
    [Required]
    public bool IsRequiredSample { get; set; } = true;
    
    /// <summary>
    /// 总分分数段类型
    /// </summary>
    [Comment("总分分数段类型")]
    public ExamScoreRangeType ExamScoreRangeType { get; set; } = ExamScoreRangeType.NONE;

    /// <summary>
    /// 备注
    /// </summary>
    [Comment("备注")]
    [StringLength(200)]
    public string Remark { get; set; } = "";


    /// <summary>
    /// 一对一引用（年级）
    /// </summary>
    public virtual Grade Grade { get; set; }
    /// <summary>
    /// 一对一引用（监测计划）
    /// </summary>
    public virtual ExamPlan ExamPlan { get; set; }

    /// <summary>
    /// 一对多引用（监测科目）
    /// </summary>
    public ICollection<ExamCourse> ExamCourses { get; } = new List<ExamCourse>();

    public void Configure(EntityTypeBuilder<ExamGrade> entityBuilder, DbContext dbContext, Type dbContextLocator)
    {
        entityBuilder.HasIndex(x => new { x.ExamPlanId, x.GradeId }).IsUnique().HasDatabaseName("idx_exam_grade_exam_plan_id_grade_id");
    }
}
