package businessapi

import (
	"context"
	"net/url"
)

// ListTemplates returns all message templates for the business account.
// Requires BusinessAccountID in the client config.
func (c *Client) ListTemplates(ctx context.Context) (*TemplateListResponse, error) {
	if c.config.BusinessAccountID == "" {
		return nil, errNoBusinessAccount
	}

	params := url.Values{
		"fields": {"id,name,status,language,category,created_at,components,rejection_reasons,quality_score"},
	}

	var resp TemplateListResponse
	if err := c.graphAPIGet(ctx, c.config.BusinessAccountID+"/message_templates", params, &resp); err != nil {
		return nil, err
	}
	return &resp, nil
}

// UploadTemplate creates a new message template.
func (c *Client) UploadTemplate(ctx context.Context, tmpl TemplateUploadRequest) (*TemplateUploadResponse, error) {
	if c.config.BusinessAccountID == "" {
		return nil, errNoBusinessAccount
	}

	var resp TemplateUploadResponse
	if err := c.graphAPIPost(ctx, c.config.BusinessAccountID+"/message_templates", tmpl, &resp); err != nil {
		return nil, err
	}
	return &resp, nil
}

// DeleteTemplate deletes a template by name and language.
func (c *Client) DeleteTemplate(ctx context.Context, name, language string) error {
	if c.config.BusinessAccountID == "" {
		return errNoBusinessAccount
	}

	params := url.Values{
		"name":     {name},
		"language": {language},
	}
	return c.graphAPIDelete(ctx, c.config.BusinessAccountID+"/message_templates", params)
}
