// Copyright © 2023 Cisco Systems, Inc. and its affiliates.
// All rights reserved.
//
// Licensed under the Mozilla Public License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://mozilla.org/MPL/2.0/
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: MPL-2.0

// Code generated by "gen/generator.go"; DO NOT EDIT.

package provider

// Section below is generated&owned by "gen/generator.go". //template:begin imports
import (
	"context"
	"fmt"

	"github.com/hashicorp/terraform-plugin-framework/datasource"
	"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
	"github.com/hashicorp/terraform-plugin-framework/path"
	"github.com/hashicorp/terraform-plugin-framework/types"
	"github.com/hashicorp/terraform-plugin-log/tflog"
)

// End of section. //template:end imports

// Section below is generated&owned by "gen/generator.go". //template:begin model

// Ensure the implementation satisfies the expected interfaces.
var (
	_ datasource.DataSource              = &ErrdisableDataSource{}
	_ datasource.DataSourceWithConfigure = &ErrdisableDataSource{}
)

func NewErrdisableDataSource() datasource.DataSource {
	return &ErrdisableDataSource{}
}

type ErrdisableDataSource struct {
	data *IosxeProviderData
}

func (d *ErrdisableDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
	resp.TypeName = req.ProviderTypeName + "_errdisable"
}

func (d *ErrdisableDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
	resp.Schema = schema.Schema{
		// This description is used by the documentation generator and the language server.
		MarkdownDescription: "This data source can read the Errdisable configuration.",

		Attributes: map[string]schema.Attribute{
			"device": schema.StringAttribute{
				MarkdownDescription: "A device name from the provider configuration.",
				Optional:            true,
			},
			"id": schema.StringAttribute{
				MarkdownDescription: "The path of the retrieved object.",
				Computed:            true,
			},
			"detect_cause_all": schema.BoolAttribute{
				MarkdownDescription: "Enable error detection on all cases",
				Computed:            true,
			},
			"detect_cause_arp_inspection": schema.BoolAttribute{
				MarkdownDescription: "Enable error detection for arp inspection",
				Computed:            true,
			},
			"detect_cause_bpduguard": schema.BoolAttribute{
				MarkdownDescription: "Enable error detection on bpdu-guard",
				Computed:            true,
			},
			"detect_cause_dhcp_rate_limit": schema.BoolAttribute{
				MarkdownDescription: "Enable error detection on dhcp-rate-limit",
				Computed:            true,
			},
			"detect_cause_dtp_flap": schema.BoolAttribute{
				MarkdownDescription: "Enable error detection on dtp-flapping",
				Computed:            true,
			},
			"detect_cause_gbic_invalid": schema.BoolAttribute{
				MarkdownDescription: "Enable error detection on gbic-invalid",
				Computed:            true,
			},
			"detect_cause_inline_power": schema.BoolAttribute{
				MarkdownDescription: "Enable error detection for inline-power",
				Computed:            true,
			},
			"detect_cause_l2ptguard": schema.BoolAttribute{
				MarkdownDescription: "Enable timer to recover from l2protocol-tunnel error disable state",
				Computed:            true,
			},
			"detect_cause_link_flap": schema.BoolAttribute{
				MarkdownDescription: "Enable error detection on linkstate-flapping",
				Computed:            true,
			},
			"detect_cause_loopback": schema.BoolAttribute{
				MarkdownDescription: "Enable error detection on loopback",
				Computed:            true,
			},
			"detect_cause_mlacp_minlink": schema.BoolAttribute{
				MarkdownDescription: "",
				Computed:            true,
			},
			"detect_cause_pagp_flap": schema.BoolAttribute{
				MarkdownDescription: "Enable error detection on pagp-flapping",
				Computed:            true,
			},
			"detect_cause_pppoe_ia_rate_limit": schema.BoolAttribute{
				MarkdownDescription: "Enable error detection on PPPoE IA rate-limit",
				Computed:            true,
			},
			"detect_cause_security_violation_shutdown_vlan": schema.BoolAttribute{
				MarkdownDescription: "Shutdown vlan on 802.1x-guard violation",
				Computed:            true,
			},
			"detect_cause_sfp_config_mismatch": schema.BoolAttribute{
				MarkdownDescription: "Enable error detection on SFP config mismatch",
				Computed:            true,
			},
			"detect_cause_small_frame": schema.BoolAttribute{
				MarkdownDescription: "Enable error detection on small_frame",
				Computed:            true,
			},
			"detect_cause_loopdetect": schema.BoolAttribute{
				MarkdownDescription: "Enable error detection on loop detect",
				Computed:            true,
			},
			"flap_setting_cause_dtp_flap_max_flaps": schema.Int64Attribute{
				MarkdownDescription: "maximum flaps allowed before setting to errdisable",
				Computed:            true,
			},
			"flap_setting_cause_dtp_flap_time": schema.Int64Attribute{
				MarkdownDescription: "time period the flaps are counted",
				Computed:            true,
			},
			"flap_setting_cause_link_flap_max_flaps": schema.Int64Attribute{
				MarkdownDescription: "maximum flaps allowed before setting to errdisable",
				Computed:            true,
			},
			"flap_setting_cause_link_flap_time": schema.Int64Attribute{
				MarkdownDescription: "time period the flaps are counted",
				Computed:            true,
			},
			"flap_setting_cause_pagp_flap_max_flaps": schema.Int64Attribute{
				MarkdownDescription: "maximum flaps allowed before setting to errdisable",
				Computed:            true,
			},
			"flap_setting_cause_pagp_flap_time": schema.Int64Attribute{
				MarkdownDescription: "time period the flaps are counted",
				Computed:            true,
			},
			"recovery_interval": schema.Int64Attribute{
				MarkdownDescription: "Error disable recovery timer value",
				Computed:            true,
			},
			"recovery_cause_all": schema.BoolAttribute{
				MarkdownDescription: "Enable timer to recover from all error causes (DEPRECATED)",
				Computed:            true,
			},
			"recovery_cause_arp_inspection": schema.BoolAttribute{
				MarkdownDescription: "Enable timer to recover from arp inspection error disable state",
				Computed:            true,
			},
			"recovery_cause_bpduguard": schema.BoolAttribute{
				MarkdownDescription: "Enable timer to recover from BPDU Guard error",
				Computed:            true,
			},
			"recovery_cause_channel_misconfig": schema.BoolAttribute{
				MarkdownDescription: "(STP)  Enable timer to recover from channel misconfiguration error",
				Computed:            true,
			},
			"recovery_cause_dhcp_rate_limit": schema.BoolAttribute{
				MarkdownDescription: "Enable timer to recover from dhcp-rate-limit error",
				Computed:            true,
			},
			"recovery_cause_dtp_flap": schema.BoolAttribute{
				MarkdownDescription: "Enable timer to recover from dtp-flap error",
				Computed:            true,
			},
			"recovery_cause_gbic_invalid": schema.BoolAttribute{
				MarkdownDescription: "Enable timer to recover from invalid GBIC error",
				Computed:            true,
			},
			"recovery_cause_inline_power": schema.BoolAttribute{
				MarkdownDescription: "Enable timer to recover from inline-power error",
				Computed:            true,
			},
			"recovery_cause_l2ptguard": schema.BoolAttribute{
				MarkdownDescription: "Enable timer to recover from l2protocol-tunnel error disable state",
				Computed:            true,
			},
			"recovery_cause_link_flap": schema.BoolAttribute{
				MarkdownDescription: "Enable timer to recover from link-flap error",
				Computed:            true,
			},
			"recovery_cause_link_monitor_failure": schema.BoolAttribute{
				MarkdownDescription: "",
				Computed:            true,
			},
			"recovery_cause_loopback": schema.BoolAttribute{
				MarkdownDescription: "Enable timer to recover from loopback error",
				Computed:            true,
			},
			"recovery_cause_mac_limit": schema.BoolAttribute{
				MarkdownDescription: "Enable timer to recover from mac limit disable state",
				Computed:            true,
			},
			"recovery_cause_mlacp_minlink": schema.BoolAttribute{
				MarkdownDescription: "",
				Computed:            true,
			},
			"recovery_cause_pagp_flap": schema.BoolAttribute{
				MarkdownDescription: "Enable timer to recover from pagp-flap error",
				Computed:            true,
			},
			"recovery_cause_port_mode_failure": schema.BoolAttribute{
				MarkdownDescription: "Enable timer to recover from port mode change failure",
				Computed:            true,
			},
			"recovery_cause_pppoe_ia_rate_limit": schema.BoolAttribute{
				MarkdownDescription: "Enable timer to recover from PPPoE IA rate-limit error",
				Computed:            true,
			},
			"recovery_cause_psp": schema.BoolAttribute{
				MarkdownDescription: "",
				Computed:            true,
			},
			"recovery_cause_psecure_violation": schema.BoolAttribute{
				MarkdownDescription: "Enable timer to recover from psecure violation error",
				Computed:            true,
			},
			"recovery_cause_security_violation": schema.BoolAttribute{
				MarkdownDescription: "Enable timer to recover from 802.1x violation error",
				Computed:            true,
			},
			"recovery_cause_sfp_config_mismatch": schema.BoolAttribute{
				MarkdownDescription: "Enable timer to recover from SFP config mismatch error",
				Computed:            true,
			},
			"recovery_cause_small_frame": schema.BoolAttribute{
				MarkdownDescription: "Enable timer to recover from small frame error",
				Computed:            true,
			},
			"recovery_cause_storm_control": schema.BoolAttribute{
				MarkdownDescription: "Enable timer to recover from storm-control error",
				Computed:            true,
			},
			"recovery_cause_udld": schema.BoolAttribute{
				MarkdownDescription: "Enable timer to recover from udld error",
				Computed:            true,
			},
			"recovery_cause_unicast_flood": schema.BoolAttribute{
				MarkdownDescription: "",
				Computed:            true,
			},
			"recovery_cause_vmps": schema.BoolAttribute{
				MarkdownDescription: "Enable timer to recover from vmps shutdown error",
				Computed:            true,
			},
			"recovery_cause_loopdetect": schema.BoolAttribute{
				MarkdownDescription: "Enable timer to recover from loop detect error",
				Computed:            true,
			},
			"recovery_cause_oam_remote_failure": schema.BoolAttribute{
				MarkdownDescription: "",
				Computed:            true,
			},
			"recovery_cause_mrp_miscabling": schema.BoolAttribute{
				MarkdownDescription: "Enable timer to recover from mrp miscabling disable state",
				Computed:            true,
			},
		},
	}
}

func (d *ErrdisableDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) {
	if req.ProviderData == nil {
		return
	}

	d.data = req.ProviderData.(*IosxeProviderData)
}

// End of section. //template:end model

// Section below is generated&owned by "gen/generator.go". //template:begin read

func (d *ErrdisableDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
	var config ErrdisableData

	// Read config
	diags := req.Config.Get(ctx, &config)
	resp.Diagnostics.Append(diags...)
	if resp.Diagnostics.HasError() {
		return
	}

	tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Read", config.getPath()))

	device, ok := d.data.Devices[config.Device.ValueString()]
	if !ok {
		resp.Diagnostics.AddAttributeError(path.Root("device"), "Invalid device", fmt.Sprintf("Device '%s' does not exist in provider configuration.", config.Device.ValueString()))
		return
	}

	res, err := device.Client.GetData(config.getPath())
	if res.StatusCode == 404 {
		config = ErrdisableData{Device: config.Device}
	} else {
		if err != nil {
			resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to retrieve object (%s), got error: %s", config.getPath(), err))
			return
		}

		config.fromBody(ctx, res.Res)
	}

	config.Id = types.StringValue(config.getPath())

	tflog.Debug(ctx, fmt.Sprintf("%s: Read finished successfully", config.getPath()))

	diags = resp.State.Set(ctx, &config)
	resp.Diagnostics.Append(diags...)
}

// End of section. //template:end read
