// 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/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              = &NetworksSwitchRoutingMulticastRendezvousPointsResource{}
	_ resource.ResourceWithConfigure = &NetworksSwitchRoutingMulticastRendezvousPointsResource{}
)

func NewNetworksSwitchRoutingMulticastRendezvousPointsResource() resource.Resource {
	return &NetworksSwitchRoutingMulticastRendezvousPointsResource{}
}

type NetworksSwitchRoutingMulticastRendezvousPointsResource struct {
	client *merakigosdk.Client
}

func (r *NetworksSwitchRoutingMulticastRendezvousPointsResource) 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 *NetworksSwitchRoutingMulticastRendezvousPointsResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
	resp.TypeName = req.ProviderTypeName + "_networks_switch_routing_multicast_rendezvous_points"
}

func (r *NetworksSwitchRoutingMulticastRendezvousPointsResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
	resp.Schema = schema.Schema{
		Attributes: map[string]schema.Attribute{
			"interface_ip": schema.StringAttribute{
				MarkdownDescription: `The IP address of the interface to use.`,
				Computed:            true,
				Optional:            true,
				PlanModifiers: []planmodifier.String{
					stringplanmodifier.UseStateForUnknown(),
				},
			},
			"interface_name": schema.StringAttribute{
				MarkdownDescription: `The name of the interface to use.`,
				Computed:            true,
			},
			"multicast_group": schema.StringAttribute{
				MarkdownDescription: `'Any', or the IP address of a multicast group.`,
				Computed:            true,
				Optional:            true,
				PlanModifiers: []planmodifier.String{
					stringplanmodifier.UseStateForUnknown(),
				},
			},
			"network_id": schema.StringAttribute{
				MarkdownDescription: `networkId path parameter. Network ID`,
				Required:            true,
			},
			"rendezvous_point_id": schema.StringAttribute{
				MarkdownDescription: `The id.`,
				Computed:            true,
				Optional:            true,
				PlanModifiers: []planmodifier.String{
					stringplanmodifier.UseStateForUnknown(),
				},
			},
			"serial": schema.StringAttribute{
				MarkdownDescription: `The serial.`,
				Computed:            true,
			},
		},
	}
}

//path params to set ['rendezvousPointId']

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

	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()
	// network_id
	//Reviw This  Has Item and item
	//HAS CREATE

	vvRendezvousPointID := data.RendezvousPointID.ValueString()
	if vvRendezvousPointID != "" {
		responseVerifyItem, restyRespGet, err := r.client.Switch.GetNetworkSwitchRoutingMulticastRendezvousPoint(vvNetworkID, vvRendezvousPointID)
		if err != nil || responseVerifyItem == nil {
			if restyRespGet != nil {
				if restyRespGet.StatusCode() != 404 {

					resp.Diagnostics.AddError(
						"Failure when executing GetNetworkSwitchRoutingMulticastRendezvousPoint",
						err.Error(),
					)
					return
				}
			}
		}

		if responseVerifyItem != nil {
			data = ResponseSwitchGetNetworkSwitchRoutingMulticastRendezvousPointItemToBodyRs(data, responseVerifyItem, false)
			diags := resp.State.Set(ctx, &data)
			resp.Diagnostics.Append(diags...)
			return
		}
	}

	response, restyResp2, err := r.client.Switch.CreateNetworkSwitchRoutingMulticastRendezvousPoint(vvNetworkID, data.toSdkApiRequestCreate(ctx))

	if err != nil || restyResp2 == nil || response == nil {
		if restyResp2 != nil {
			resp.Diagnostics.AddError(
				"Failure when executing ",
				"Status: "+strconv.Itoa(restyResp2.StatusCode())+"\n"+restyResp2.String(),
			)
			return
		}
		resp.Diagnostics.AddError(
			"Failure when executing ",
			err.Error(),
		)
		return
	}
	//Items
	vvRendezvousPointID = response.RendezvousPointID
	responseGet, restyResp1, err := r.client.Switch.GetNetworkSwitchRoutingMulticastRendezvousPoint(vvNetworkID, vvRendezvousPointID)
	// Has item and has items

	if err != nil || responseGet == nil {
		if restyResp1 != nil {
			resp.Diagnostics.AddError(
				"Failure when executing GetNetworkSwitchRoutingMulticastRendezvousPoints",
				restyResp1.String(),
			)
			return
		}
		resp.Diagnostics.AddError(
			"Failure when executing GetNetworkSwitchRoutingMulticastRendezvousPoints",
			err.Error(),
		)
		return
	} else {
		data = ResponseSwitchGetNetworkSwitchRoutingMulticastRendezvousPointItemToBodyRs(data, responseGet, false)
		resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
		return
	}
}

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

	var item types.Object

	resp.Diagnostics.Append(req.State.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
	// Has Item2

	vvNetworkID := data.NetworkID.ValueString()
	vvRendezvousPointID := data.RendezvousPointID.ValueString()
	responseGet, restyRespGet, err := r.client.Switch.GetNetworkSwitchRoutingMulticastRendezvousPoint(vvNetworkID, vvRendezvousPointID)
	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 GetNetworkSwitchRoutingMulticastRendezvousPoint",
				restyRespGet.String(),
			)
			return
		}
		resp.Diagnostics.AddError(
			"Failure when executing GetNetworkSwitchRoutingMulticastRendezvousPoint",
			err.Error(),
		)
		return
	}
	//entro aqui 2
	data = ResponseSwitchGetNetworkSwitchRoutingMulticastRendezvousPointItemToBodyRs(data, responseGet, true)
	diags := resp.State.Set(ctx, &data)
	//update path params assigned
	resp.Diagnostics.Append(diags...)
}
func (r *NetworksSwitchRoutingMulticastRendezvousPointsResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
	idParts := strings.Split(req.ID, ",")

	if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" {
		resp.Diagnostics.AddError(
			"Unexpected Import Identifier",
			fmt.Sprintf("Expected import identifier with format: attr_one,attr_two. 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("rendezvous_point_id"), idParts[1])...)
}

func (r *NetworksSwitchRoutingMulticastRendezvousPointsResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
	var data NetworksSwitchRoutingMulticastRendezvousPointsRs
	merge(ctx, req, resp, &data)

	if resp.Diagnostics.HasError() {
		return
	}
	//Has Paths
	//Update

	//Path Params
	vvNetworkID := data.NetworkID.ValueString()
	vvRendezvousPointID := data.RendezvousPointID.ValueString()
	dataRequest := data.toSdkApiRequestUpdate(ctx)
	response, restyResp2, err := r.client.Switch.UpdateNetworkSwitchRoutingMulticastRendezvousPoint(vvNetworkID, vvRendezvousPointID, dataRequest)
	if err != nil || restyResp2 == nil || response == nil {
		if restyResp2 != nil {
			resp.Diagnostics.AddError(
				"Failure when executing UpdateNetworkSwitchRoutingMulticastRendezvousPoint",
				"Status: "+strconv.Itoa(restyResp2.StatusCode())+"\n"+restyResp2.String(),
			)
			return
		}
		resp.Diagnostics.AddError(
			"Failure when executing UpdateNetworkSwitchRoutingMulticastRendezvousPoint",
			err.Error(),
		)
		return
	}
	resp.Diagnostics.Append(req.Plan.Set(ctx, &data)...)
	diags := resp.State.Set(ctx, &data)
	resp.Diagnostics.Append(diags...)
}

func (r *NetworksSwitchRoutingMulticastRendezvousPointsResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
	var state NetworksSwitchRoutingMulticastRendezvousPointsRs
	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()
	vvRendezvousPointID := state.RendezvousPointID.ValueString()
	_, err := r.client.Switch.DeleteNetworkSwitchRoutingMulticastRendezvousPoint(vvNetworkID, vvRendezvousPointID)
	if err != nil {
		resp.Diagnostics.AddError(
			"Failure when executing DeleteNetworkSwitchRoutingMulticastRendezvousPoint", err.Error())
		return
	}

	resp.State.RemoveResource(ctx)

}

// TF Structs Schema
type NetworksSwitchRoutingMulticastRendezvousPointsRs struct {
	NetworkID         types.String `tfsdk:"network_id"`
	RendezvousPointID types.String `tfsdk:"rendezvous_point_id"`
	InterfaceIP       types.String `tfsdk:"interface_ip"`
	InterfaceName     types.String `tfsdk:"interface_name"`
	MulticastGroup    types.String `tfsdk:"multicast_group"`
	Serial            types.String `tfsdk:"serial"`
}

// FromBody
func (r *NetworksSwitchRoutingMulticastRendezvousPointsRs) toSdkApiRequestCreate(ctx context.Context) *merakigosdk.RequestSwitchCreateNetworkSwitchRoutingMulticastRendezvousPoint {
	emptyString := ""
	interfaceIP := new(string)
	if !r.InterfaceIP.IsUnknown() && !r.InterfaceIP.IsNull() {
		*interfaceIP = r.InterfaceIP.ValueString()
	} else {
		interfaceIP = &emptyString
	}
	multicastGroup := new(string)
	if !r.MulticastGroup.IsUnknown() && !r.MulticastGroup.IsNull() {
		*multicastGroup = r.MulticastGroup.ValueString()
	} else {
		multicastGroup = &emptyString
	}
	out := merakigosdk.RequestSwitchCreateNetworkSwitchRoutingMulticastRendezvousPoint{
		InterfaceIP:    *interfaceIP,
		MulticastGroup: *multicastGroup,
	}
	return &out
}
func (r *NetworksSwitchRoutingMulticastRendezvousPointsRs) toSdkApiRequestUpdate(ctx context.Context) *merakigosdk.RequestSwitchUpdateNetworkSwitchRoutingMulticastRendezvousPoint {
	emptyString := ""
	interfaceIP := new(string)
	if !r.InterfaceIP.IsUnknown() && !r.InterfaceIP.IsNull() {
		*interfaceIP = r.InterfaceIP.ValueString()
	} else {
		interfaceIP = &emptyString
	}
	multicastGroup := new(string)
	if !r.MulticastGroup.IsUnknown() && !r.MulticastGroup.IsNull() {
		*multicastGroup = r.MulticastGroup.ValueString()
	} else {
		multicastGroup = &emptyString
	}
	out := merakigosdk.RequestSwitchUpdateNetworkSwitchRoutingMulticastRendezvousPoint{
		InterfaceIP:    *interfaceIP,
		MulticastGroup: *multicastGroup,
	}
	return &out
}

// From gosdk to TF Structs Schema
func ResponseSwitchGetNetworkSwitchRoutingMulticastRendezvousPointItemToBodyRs(state NetworksSwitchRoutingMulticastRendezvousPointsRs, response *merakigosdk.ResponseSwitchGetNetworkSwitchRoutingMulticastRendezvousPoint, is_read bool) NetworksSwitchRoutingMulticastRendezvousPointsRs {
	itemState := NetworksSwitchRoutingMulticastRendezvousPointsRs{
		InterfaceIP: func() types.String {
			if response.InterfaceIP != "" {
				return types.StringValue(response.InterfaceIP)
			}
			return types.String{}
		}(),
		InterfaceName: func() types.String {
			if response.InterfaceName != "" {
				return types.StringValue(response.InterfaceName)
			}
			return types.String{}
		}(),
		MulticastGroup: func() types.String {
			if response.MulticastGroup != "" {
				return types.StringValue(response.MulticastGroup)
			}
			return types.String{}
		}(),
		RendezvousPointID: func() types.String {
			if response.RendezvousPointID != "" {
				return types.StringValue(response.RendezvousPointID)
			}
			return types.String{}
		}(),
		Serial: func() types.String {
			if response.Serial != "" {
				return types.StringValue(response.Serial)
			}
			return types.String{}
		}(),
	}
	if is_read {
		return mergeInterfacesOnlyPath(state, itemState).(NetworksSwitchRoutingMulticastRendezvousPointsRs)
	}
	return mergeInterfaces(state, itemState, true).(NetworksSwitchRoutingMulticastRendezvousPointsRs)
}
