package schemas

import (
	"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
	"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
	"github.com/hashicorp/terraform-plugin-framework/resource/schema"
	"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
	"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
	"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
	"github.com/hashicorp/terraform-plugin-framework/schema/validator"
	"github.com/hashicorp/terraform-plugin-framework/types"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/constants"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/validate"
)

func MakeSchemaResourceBServiceGroup() map[string]schema.Attribute {
	return map[string]schema.Attribute{
		"service_id": schema.Int64Attribute{
			Required: true,
		},
		"compgroup_name": schema.StringAttribute{
			Required: true,
		},
		"comp_count": schema.Int64Attribute{
			Required: true,
		},
		"cpu": schema.Int64Attribute{
			Required: true,
		},
		"ram": schema.Int64Attribute{
			Required: true,
			Validators: []validator.Int64{
				validate.DivisibleBy(constants.DivisibleByRAM),
				int64validator.AtLeast(constants.MinRamPerCompute),
			},
		},
		"disk": schema.Int64Attribute{
			Required: true,
		},
		"image_id": schema.Int64Attribute{
			Required: true,
		},
		"driver": schema.StringAttribute{
			Required: true,
		},
		"chipset": schema.StringAttribute{
			Optional: true,
			Validators: []validator.String{
				stringvalidator.OneOf("i440fx", "Q35"),
			},
			Description: "Type of the emulated system, Q35 or i440fx",
		},
		"sep_id": schema.Int64Attribute{
			Optional: true,
			Computed: true,
		},
		"sep_pool": schema.StringAttribute{
			Optional: true,
			Computed: true,
		},
		"cloud_init": schema.StringAttribute{
			Optional: true,
		},
		"role": schema.StringAttribute{
			Optional: true,
			Computed: true,
		},
		"timeout_start": schema.Int64Attribute{
			Optional: true,
			Computed: true,
		},
		"extnets": schema.ListAttribute{
			Optional:    true,
			Computed:    true,
			ElementType: types.Int64Type,
		},
		"vinses": schema.ListAttribute{
			Optional:    true,
			Computed:    true,
			ElementType: types.Int64Type,
		},
		"mode": schema.StringAttribute{
			Optional: true,
			Validators: []validator.String{
				stringvalidator.OneOf("RELATIVE", "ABSOLUTE"),
			},
		},
		"start": schema.BoolAttribute{
			Optional: true,
			Computed: true,
			Default:  booldefault.StaticBool(false),
		},
		"force_stop": schema.BoolAttribute{
			Optional: true,
			Computed: true,
			Default:  booldefault.StaticBool(false),
		},
		"force_update": schema.BoolAttribute{
			Optional: true,
			Computed: true,
			Default:  booldefault.StaticBool(false),
		},
		"parents": schema.ListAttribute{
			Optional:    true,
			Computed:    true,
			ElementType: types.Int64Type,
		},
		"remove_computes": schema.ListAttribute{
			Optional:    true,
			ElementType: types.Int64Type,
		},
		"compgroup_id": schema.Int64Attribute{
			Optional: true,
			Computed: true,
		},
		"account_id": schema.Int64Attribute{
			Computed: true,
		},
		"account_name": schema.StringAttribute{
			Computed: true,
		},
		"computes": schema.ListNestedAttribute{
			Computed: true,
			NestedObject: schema.NestedAttributeObject{
				Attributes: map[string]schema.Attribute{
					"id": schema.Int64Attribute{
						Computed: true,
					},
					"ip_addresses": schema.ListAttribute{
						Computed:    true,
						ElementType: types.StringType,
					},
					"name": schema.StringAttribute{
						Computed: true,
					},
					"chipset": schema.StringAttribute{
						Computed:    true,
						Description: "Type of the emulated system, Q35 or i440fx",
					},
					"os_users": schema.ListNestedAttribute{
						Computed: true,
						NestedObject: schema.NestedAttributeObject{
							Attributes: map[string]schema.Attribute{
								"login": schema.StringAttribute{
									Computed: true,
								},
								"password": schema.StringAttribute{
									Computed: true,
								},
							},
						},
					},
				},
			},
		},
		"consistency": schema.BoolAttribute{
			Computed: true,
		},
		"created_by": schema.StringAttribute{
			Computed: true,
		},
		"created_time": schema.Int64Attribute{
			Computed: true,
		},
		"deleted_by": schema.StringAttribute{
			Computed: true,
		},
		"deleted_time": schema.Int64Attribute{
			Computed: true,
		},
		"gid": schema.Int64Attribute{
			Computed: true,
		},
		"guid": schema.Int64Attribute{
			Computed: true,
		},
		"milestones": schema.Int64Attribute{
			Computed: true,
		},
		"rg_id": schema.Int64Attribute{
			Computed: true,
		},
		"rg_name": schema.StringAttribute{
			Computed: true,
		},
		"seq_no": schema.Int64Attribute{
			Computed: true,
		},
		"status": schema.StringAttribute{
			Computed: true,
		},
		"tech_status": schema.StringAttribute{
			Computed: true,
		},
		"updated_by": schema.StringAttribute{
			Computed: true,
		},
		"updated_time": schema.Int64Attribute{
			Computed: true,
		},
		"id": schema.StringAttribute{
			Computed: true,
			PlanModifiers: []planmodifier.String{
				stringplanmodifier.UseStateForUnknown(),
			},
		},
		"sid": schema.StringAttribute{
			Computed: true,
			PlanModifiers: []planmodifier.String{
				stringplanmodifier.UseStateForUnknown(),
			},
		},
	}
}
