// 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
package provider

// RESOURCE NORMAL
import (
	"context"
	"fmt"
	"strconv"
	"strings"

	merakigosdk "github.com/meraki/dashboard-api-go/v5/sdk"

	"github.com/hashicorp/terraform-plugin-framework/path"
	"github.com/hashicorp/terraform-plugin-framework/resource"
	"github.com/hashicorp/terraform-plugin-framework/resource/schema"
	"github.com/hashicorp/terraform-plugin-framework/resource/schema/boolplanmodifier"
	"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
	"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
	"github.com/hashicorp/terraform-plugin-framework/types"
	"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
)

var (
	_ resource.Resource              = &NetworksSwitchStacksRoutingStaticRoutesResource{}
	_ resource.ResourceWithConfigure = &NetworksSwitchStacksRoutingStaticRoutesResource{}
)

func NewNetworksSwitchStacksRoutingStaticRoutesResource() resource.Resource {
	return &NetworksSwitchStacksRoutingStaticRoutesResource{}
}

type NetworksSwitchStacksRoutingStaticRoutesResource struct {
	client *merakigosdk.Client
}

func (r *NetworksSwitchStacksRoutingStaticRoutesResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
	if req.ProviderData == nil {
		return
	}
	client := req.ProviderData.(MerakiProviderData).Client
	r.client = client
}

// Metadata returns the data source type name.
func (r *NetworksSwitchStacksRoutingStaticRoutesResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
	resp.TypeName = req.ProviderTypeName + "_networks_switch_stacks_routing_static_routes"
}

func (r *NetworksSwitchStacksRoutingStaticRoutesResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
	resp.Schema = schema.Schema{
		Attributes: map[string]schema.Attribute{
			"advertise_via_ospf_enabled": schema.BoolAttribute{
				MarkdownDescription: `Option to advertise static routes via OSPF`,
				Optional:            true,
				PlanModifiers: []planmodifier.Bool{
					boolplanmodifier.UseStateForUnknown(),
				},
			},
			"management_next_hop": schema.StringAttribute{
				MarkdownDescription: `Optional fallback IP address for management traffic`,
				Optional:            true,
				PlanModifiers: []planmodifier.String{
					stringplanmodifier.UseStateForUnknown(),
				},
			},
			"name": schema.StringAttribute{
				MarkdownDescription: `The name or description of the layer 3 static route`,
				Optional:            true,
				PlanModifiers: []planmodifier.String{
					stringplanmodifier.UseStateForUnknown(),
				},
			},
			"network_id": schema.StringAttribute{
				MarkdownDescription: `networkId path parameter. Network ID`,
				Required:            true,
			},
			"next_hop_ip": schema.StringAttribute{
				MarkdownDescription: `The IP address of the router to which traffic for this destination network should be sent`,
				Optional:            true,
				PlanModifiers: []planmodifier.String{
					stringplanmodifier.UseStateForUnknown(),
				},
			},
			"prefer_over_ospf_routes_enabled": schema.BoolAttribute{
				MarkdownDescription: `Option to prefer static routes over OSPF routes`,
				Optional:            true,
				PlanModifiers: []planmodifier.Bool{
					boolplanmodifier.UseStateForUnknown(),
				},
			},
			"static_route_id": schema.StringAttribute{
				MarkdownDescription: `The identifier of a layer 3 static route`,
				Optional:            true,
				PlanModifiers: []planmodifier.String{
					stringplanmodifier.UseStateForUnknown(),
				},
			},
			"subnet": schema.StringAttribute{
				MarkdownDescription: `The IP address of the subnetwork specified in CIDR notation (ex. 1.2.3.0/24)`,
				Optional:            true,
				PlanModifiers: []planmodifier.String{
					stringplanmodifier.UseStateForUnknown(),
				},
			},
			"switch_stack_id": schema.StringAttribute{
				MarkdownDescription: `switchStackId path parameter. Switch stack ID`,
				Required:            true,
			},
		},
	}
}

//path params to set ['staticRouteId']

func (r *NetworksSwitchStacksRoutingStaticRoutesResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
	// Retrieve values from plan
	var data NetworksSwitchStacksRoutingStaticRoutesRs

	var item types.Object
	resp.Diagnostics.Append(req.Plan.Get(ctx, &item)...)
	if resp.Diagnostics.HasError() {
		return
	}

	resp.Diagnostics.Append(item.As(ctx, &data, basetypes.ObjectAsOptions{
		UnhandledNullAsEmpty:    true,
		UnhandledUnknownAsEmpty: true,
	})...)

	if resp.Diagnostics.HasError() {
		return
	}
	// Has Paths
	vvNetworkID := data.NetworkID.ValueString()
	vvSwitchStackID := data.SwitchStackID.ValueString()
	//Has Item and has items and post

	vvName := data.Name.ValueString()

	responseVerifyItem, restyResp1, err := r.client.Switch.GetNetworkSwitchStackRoutingStaticRoutes(vvNetworkID, vvSwitchStackID)
	//Has Post
	if err != nil {
		if restyResp1 != nil {
			if restyResp1.StatusCode() != 404 {
				resp.Diagnostics.AddError(
					"Failure when executing GetNetworkSwitchStackRoutingStaticRoutes",
					restyResp1.String(),
				)
				return
			}
		}
	}

	if responseVerifyItem != nil {
		responseStruct := structToMap(responseVerifyItem)
		result := getDictResult(responseStruct, "Name", vvName, simpleCmp)
		if result != nil {
			result2 := result.(map[string]interface{})
			vvStaticRouteID, ok := result2["StaticRouteID"].(string)
			if !ok {
				resp.Diagnostics.AddError(
					"Failure when parsing path parameter StaticRouteID",
					"Fail Parsing StaticRouteID",
				)
				return
			}
			r.client.Switch.UpdateNetworkSwitchStackRoutingStaticRoute(vvNetworkID, vvSwitchStackID, vvStaticRouteID, data.toSdkApiRequestUpdate(ctx))

			responseVerifyItem2, _, _ := r.client.Switch.GetNetworkSwitchStackRoutingStaticRoute(vvNetworkID, vvSwitchStackID, vvStaticRouteID)
			if responseVerifyItem2 != nil {
				data = ResponseSwitchGetNetworkSwitchStackRoutingStaticRouteItemToBodyRs(data, responseVerifyItem2, false)
				// Path params update assigned
				resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
				return
			}
		}
	}

	dataRequest := data.toSdkApiRequestCreate(ctx)
	response, restyResp2, err := r.client.Switch.CreateNetworkSwitchStackRoutingStaticRoute(vvNetworkID, vvSwitchStackID, dataRequest)
	if err != nil || restyResp2 == nil || response == nil {
		if restyResp2 != nil {
			resp.Diagnostics.AddError(
				"Failure when executing CreateNetworkSwitchStackRoutingStaticRoute",
				"Status: "+strconv.Itoa(restyResp2.StatusCode())+"\n"+restyResp2.String(),
			)
			return
		}
		resp.Diagnostics.AddError(
			"Failure when executing CreateNetworkSwitchStackRoutingStaticRoute",
			err.Error(),
		)
		return
	}

	responseGet, restyResp1, err := r.client.Switch.GetNetworkSwitchStackRoutingStaticRoutes(vvNetworkID, vvSwitchStackID)

	if err != nil || responseGet == nil {
		if restyResp1 != nil {
			resp.Diagnostics.AddError(
				"Failure when executing GetNetworkSwitchStackRoutingStaticRoutes",
				restyResp1.String(),
			)
			return
		}
		resp.Diagnostics.AddError(
			"Failure when executing GetNetworkSwitchStackRoutingStaticRoutes",
			err.Error(),
		)
		return
	}

	responseStruct := structToMap(responseGet)
	result := getDictResult(responseStruct, "Name", vvName, simpleCmp)
	if result != nil {
		result2 := result.(map[string]interface{})
		vvStaticRouteID, ok := result2["StaticRouteID"].(string)
		if !ok {
			resp.Diagnostics.AddError(
				"Failure when parsing path parameter StaticRouteID",
				"Fail Parsing StaticRouteID",
			)
			return
		}
		responseVerifyItem2, restyRespGet, err := r.client.Switch.GetNetworkSwitchStackRoutingStaticRoute(vvNetworkID, vvSwitchStackID, vvStaticRouteID)
		if responseVerifyItem2 != nil && err == nil {
			data = ResponseSwitchGetNetworkSwitchStackRoutingStaticRouteItemToBodyRs(data, responseVerifyItem2, false)
			resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
			return
		} else {
			if restyRespGet != nil {
				resp.Diagnostics.AddError(
					"Failure when executing GetNetworkSwitchStackRoutingStaticRoute",
					restyRespGet.String(),
				)
				return
			}
			resp.Diagnostics.AddError(
				"Failure when executing GetNetworkSwitchStackRoutingStaticRoute",
				err.Error(),
			)
			return
		}
	} else {
		resp.Diagnostics.AddError(
			"Error in result.",
			"Error in result.",
		)
		return
	}

}

func (r *NetworksSwitchStacksRoutingStaticRoutesResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
	var data NetworksSwitchStacksRoutingStaticRoutesRs

	diags := req.State.Get(ctx, &data)
	if resp.Diagnostics.Append(diags...); resp.Diagnostics.HasError() {
		return
	}

	//Has Paths
	// Has Item2

	vvNetworkID := data.NetworkID.ValueString()
	vvSwitchStackID := data.SwitchStackID.ValueString()
	vvStaticRouteID := data.StaticRouteID.ValueString()
	responseGet, restyRespGet, err := r.client.Switch.GetNetworkSwitchStackRoutingStaticRoute(vvNetworkID, vvSwitchStackID, vvStaticRouteID)
	if err != nil || restyRespGet == nil {
		if restyRespGet != nil {
			if restyRespGet.StatusCode() == 404 {
				resp.Diagnostics.AddWarning(
					"Resource not found",
					"Deleting resource",
				)
				resp.State.RemoveResource(ctx)
				return
			}
			resp.Diagnostics.AddError(
				"Failure when executing GetNetworkSwitchStackRoutingStaticRoute",
				restyRespGet.String(),
			)
			return
		}
		resp.Diagnostics.AddError(
			"Failure when executing GetNetworkSwitchStackRoutingStaticRoute",
			err.Error(),
		)
		return
	}
	//entro aqui 2
	data = ResponseSwitchGetNetworkSwitchStackRoutingStaticRouteItemToBodyRs(data, responseGet, true)
	resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r *NetworksSwitchStacksRoutingStaticRoutesResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
	idParts := strings.Split(req.ID, ",")

	if len(idParts) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" {
		resp.Diagnostics.AddError(
			"Unexpected Import Identifier",
			fmt.Sprintf("Expected import identifier with format: networkId,switchStackId,staticRouteId. Got: %q", req.ID),
		)
		return
	}
	resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("network_id"), idParts[0])...)
	resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("switch_stack_id"), idParts[1])...)
	resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("static_route_id"), idParts[2])...)
}

func (r *NetworksSwitchStacksRoutingStaticRoutesResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
	var plan NetworksSwitchStacksRoutingStaticRoutesRs
	merge(ctx, req, resp, &plan)

	if resp.Diagnostics.HasError() {
		return
	}
	//Path Params
	vvNetworkID := plan.NetworkID.ValueString()
	vvSwitchStackID := plan.SwitchStackID.ValueString()
	vvStaticRouteID := plan.StaticRouteID.ValueString()
	dataRequest := plan.toSdkApiRequestUpdate(ctx)
	response, restyResp2, err := r.client.Switch.UpdateNetworkSwitchStackRoutingStaticRoute(vvNetworkID, vvSwitchStackID, vvStaticRouteID, dataRequest)
	if err != nil || restyResp2 == nil || response == nil {
		if restyResp2 != nil {
			resp.Diagnostics.AddError(
				"Failure when executing UpdateNetworkSwitchStackRoutingStaticRoute",
				"Status: "+strconv.Itoa(restyResp2.StatusCode())+"\n"+restyResp2.String(),
			)
			return
		}
		resp.Diagnostics.AddError(
			"Failure when executing UpdateNetworkSwitchStackRoutingStaticRoute",
			err.Error(),
		)
		return
	}
	resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
}

func (r *NetworksSwitchStacksRoutingStaticRoutesResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
	var state NetworksSwitchStacksRoutingStaticRoutesRs
	var item types.Object

	resp.Diagnostics.Append(req.State.Get(ctx, &item)...)
	if resp.Diagnostics.HasError() {
		return
	}

	resp.Diagnostics.Append(item.As(ctx, &state, basetypes.ObjectAsOptions{
		UnhandledNullAsEmpty:    true,
		UnhandledUnknownAsEmpty: true,
	})...)
	if resp.Diagnostics.HasError() {
		return
	}

	vvNetworkID := state.NetworkID.ValueString()
	vvSwitchStackID := state.SwitchStackID.ValueString()
	vvStaticRouteID := state.StaticRouteID.ValueString()
	_, err := r.client.Switch.DeleteNetworkSwitchStackRoutingStaticRoute(vvNetworkID, vvSwitchStackID, vvStaticRouteID)
	if err != nil {
		resp.Diagnostics.AddError(
			"Failure when executing DeleteNetworkSwitchStackRoutingStaticRoute", err.Error())
		return
	}

	resp.State.RemoveResource(ctx)

}

// TF Structs Schema
type NetworksSwitchStacksRoutingStaticRoutesRs struct {
	NetworkID                   types.String `tfsdk:"network_id"`
	SwitchStackID               types.String `tfsdk:"switch_stack_id"`
	StaticRouteID               types.String `tfsdk:"static_route_id"`
	AdvertiseViaOspfEnabled     types.Bool   `tfsdk:"advertise_via_ospf_enabled"`
	ManagementNextHop           types.String `tfsdk:"management_next_hop"`
	Name                        types.String `tfsdk:"name"`
	NextHopIP                   types.String `tfsdk:"next_hop_ip"`
	PreferOverOspfRoutesEnabled types.Bool   `tfsdk:"prefer_over_ospf_routes_enabled"`
	Subnet                      types.String `tfsdk:"subnet"`
}

// FromBody
func (r *NetworksSwitchStacksRoutingStaticRoutesRs) toSdkApiRequestCreate(ctx context.Context) *merakigosdk.RequestSwitchCreateNetworkSwitchStackRoutingStaticRoute {
	emptyString := ""
	advertiseViaOspfEnabled := new(bool)
	if !r.AdvertiseViaOspfEnabled.IsUnknown() && !r.AdvertiseViaOspfEnabled.IsNull() {
		*advertiseViaOspfEnabled = r.AdvertiseViaOspfEnabled.ValueBool()
	} else {
		advertiseViaOspfEnabled = nil
	}
	name := new(string)
	if !r.Name.IsUnknown() && !r.Name.IsNull() {
		*name = r.Name.ValueString()
	} else {
		name = &emptyString
	}
	nextHopIP := new(string)
	if !r.NextHopIP.IsUnknown() && !r.NextHopIP.IsNull() {
		*nextHopIP = r.NextHopIP.ValueString()
	} else {
		nextHopIP = &emptyString
	}
	preferOverOspfRoutesEnabled := new(bool)
	if !r.PreferOverOspfRoutesEnabled.IsUnknown() && !r.PreferOverOspfRoutesEnabled.IsNull() {
		*preferOverOspfRoutesEnabled = r.PreferOverOspfRoutesEnabled.ValueBool()
	} else {
		preferOverOspfRoutesEnabled = nil
	}
	subnet := new(string)
	if !r.Subnet.IsUnknown() && !r.Subnet.IsNull() {
		*subnet = r.Subnet.ValueString()
	} else {
		subnet = &emptyString
	}
	out := merakigosdk.RequestSwitchCreateNetworkSwitchStackRoutingStaticRoute{
		AdvertiseViaOspfEnabled:     advertiseViaOspfEnabled,
		Name:                        *name,
		NextHopIP:                   *nextHopIP,
		PreferOverOspfRoutesEnabled: preferOverOspfRoutesEnabled,
		Subnet:                      *subnet,
	}
	return &out
}
func (r *NetworksSwitchStacksRoutingStaticRoutesRs) toSdkApiRequestUpdate(ctx context.Context) *merakigosdk.RequestSwitchUpdateNetworkSwitchStackRoutingStaticRoute {
	emptyString := ""
	advertiseViaOspfEnabled := new(bool)
	if !r.AdvertiseViaOspfEnabled.IsUnknown() && !r.AdvertiseViaOspfEnabled.IsNull() {
		*advertiseViaOspfEnabled = r.AdvertiseViaOspfEnabled.ValueBool()
	} else {
		advertiseViaOspfEnabled = nil
	}
	managementNextHop := new(string)
	if !r.ManagementNextHop.IsUnknown() && !r.ManagementNextHop.IsNull() {
		*managementNextHop = r.ManagementNextHop.ValueString()
	} else {
		managementNextHop = &emptyString
	}
	name := new(string)
	if !r.Name.IsUnknown() && !r.Name.IsNull() {
		*name = r.Name.ValueString()
	} else {
		name = &emptyString
	}
	nextHopIP := new(string)
	if !r.NextHopIP.IsUnknown() && !r.NextHopIP.IsNull() {
		*nextHopIP = r.NextHopIP.ValueString()
	} else {
		nextHopIP = &emptyString
	}
	preferOverOspfRoutesEnabled := new(bool)
	if !r.PreferOverOspfRoutesEnabled.IsUnknown() && !r.PreferOverOspfRoutesEnabled.IsNull() {
		*preferOverOspfRoutesEnabled = r.PreferOverOspfRoutesEnabled.ValueBool()
	} else {
		preferOverOspfRoutesEnabled = nil
	}
	subnet := new(string)
	if !r.Subnet.IsUnknown() && !r.Subnet.IsNull() {
		*subnet = r.Subnet.ValueString()
	} else {
		subnet = &emptyString
	}
	out := merakigosdk.RequestSwitchUpdateNetworkSwitchStackRoutingStaticRoute{
		AdvertiseViaOspfEnabled:     advertiseViaOspfEnabled,
		ManagementNextHop:           *managementNextHop,
		Name:                        *name,
		NextHopIP:                   *nextHopIP,
		PreferOverOspfRoutesEnabled: preferOverOspfRoutesEnabled,
		Subnet:                      *subnet,
	}
	return &out
}

// From gosdk to TF Structs Schema
func ResponseSwitchGetNetworkSwitchStackRoutingStaticRouteItemToBodyRs(state NetworksSwitchStacksRoutingStaticRoutesRs, response *merakigosdk.ResponseSwitchGetNetworkSwitchStackRoutingStaticRoute, is_read bool) NetworksSwitchStacksRoutingStaticRoutesRs {
	itemState := NetworksSwitchStacksRoutingStaticRoutesRs{
		AdvertiseViaOspfEnabled: func() types.Bool {
			if response.AdvertiseViaOspfEnabled != nil {
				return types.BoolValue(*response.AdvertiseViaOspfEnabled)
			}
			return types.Bool{}
		}(),
		ManagementNextHop: func() types.String {
			if response.ManagementNextHop != "" {
				return types.StringValue(response.ManagementNextHop)
			}
			return types.String{}
		}(),
		Name: func() types.String {
			if response.Name != "" {
				return types.StringValue(response.Name)
			}
			return types.String{}
		}(),
		NextHopIP: func() types.String {
			if response.NextHopIP != "" {
				return types.StringValue(response.NextHopIP)
			}
			return types.String{}
		}(),
		PreferOverOspfRoutesEnabled: func() types.Bool {
			if response.PreferOverOspfRoutesEnabled != nil {
				return types.BoolValue(*response.PreferOverOspfRoutesEnabled)
			}
			return types.Bool{}
		}(),
		StaticRouteID: func() types.String {
			if response.StaticRouteID != "" {
				return types.StringValue(response.StaticRouteID)
			}
			return types.String{}
		}(),
		Subnet: func() types.String {
			if response.Subnet != "" {
				return types.StringValue(response.Subnet)
			}
			return types.String{}
		}(),
	}
	if is_read {
		return mergeInterfacesOnlyPath(state, itemState).(NetworksSwitchStacksRoutingStaticRoutesRs)
	}
	return mergeInterfaces(state, itemState, true).(NetworksSwitchStacksRoutingStaticRoutesRs)
}
