// Code generated by templ - DO NOT EDIT.

// templ: version: v0.3.977
package templates

//lint:file-ignore SA4006 This context is only used if a nested component is present.

import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"

func CollectionRules(user User, collection CollectionData) templ.Component {
	return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
		templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
		if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
			return templ_7745c5c3_CtxErr
		}
		templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
		if !templ_7745c5c3_IsBuffer {
			defer func() {
				templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
				if templ_7745c5c3_Err == nil {
					templ_7745c5c3_Err = templ_7745c5c3_BufErr
				}
			}()
		}
		ctx = templ.InitializeContext(ctx)
		templ_7745c5c3_Var1 := templ.GetChildren(ctx)
		if templ_7745c5c3_Var1 == nil {
			templ_7745c5c3_Var1 = templ.NopComponent
		}
		ctx = templ.ClearChildren(ctx)
		templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<!doctype html><html lang=\"en\"><head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><title>Collection Rules - ")
		if templ_7745c5c3_Err != nil {
			return templ_7745c5c3_Err
		}
		var templ_7745c5c3_Var2 string
		templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(collection.Name)
		if templ_7745c5c3_Err != nil {
			return templ.Error{Err: templ_7745c5c3_Err, FileName: `templates/collection_rules.templ`, Line: 9, Col: 51}
		}
		_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
		if templ_7745c5c3_Err != nil {
			return templ_7745c5c3_Err
		}
		templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, " - Bookhoard</title><script src=\"/static/htmx.min.js\"></script><script src=\"/static/toast.js\"></script><script src=\"/static/header.js\"></script><link href=\"/static/style.css\" rel=\"stylesheet\"></head><body class=\"theme-{ user.Theme }\">")
		if templ_7745c5c3_Err != nil {
			return templ_7745c5c3_Err
		}
		templ_7745c5c3_Err = Header(user, "/collections").Render(ctx, templ_7745c5c3_Buffer)
		if templ_7745c5c3_Err != nil {
			return templ_7745c5c3_Err
		}
		templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "<div class=\"max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8\"><div class=\"mb-6\"><button onclick=\"backToCollection()\" class=\"btn-secondary px-4 py-2 rounded-lg mb-4\">← Back to Collection</button><div class=\"flex items-center gap-4\"><div class=\"text-4xl\" style=\"color: { collection.Color }\">")
		if templ_7745c5c3_Err != nil {
			return templ_7745c5c3_Err
		}
		var templ_7745c5c3_Var3 string
		templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(collection.Icon)
		if templ_7745c5c3_Err != nil {
			return templ.Error{Err: templ_7745c5c3_Err, FileName: `templates/collection_rules.templ`, Line: 24, Col: 95}
		}
		_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
		if templ_7745c5c3_Err != nil {
			return templ_7745c5c3_Err
		}
		templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "</div><div><h1 class=\"text-3xl font-bold\" style=\"color: var(--text-primary)\">")
		if templ_7745c5c3_Err != nil {
			return templ_7745c5c3_Err
		}
		var templ_7745c5c3_Var4 string
		templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(collection.Name)
		if templ_7745c5c3_Err != nil {
			return templ.Error{Err: templ_7745c5c3_Err, FileName: `templates/collection_rules.templ`, Line: 26, Col: 107}
		}
		_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4))
		if templ_7745c5c3_Err != nil {
			return templ_7745c5c3_Err
		}
		templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "</h1><p style=\"color: var(--text-secondary)\">Auto-Assign Rules</p></div></div></div><div class=\"mb-8\"><h2 class=\"text-2xl font-bold mb-4\" style=\"color: var(--text-primary)\">Existing Rules</h2><div id=\"rules-container\" class=\"space-y-4\"><div id=\"no-rules\" class=\"card p-6 rounded-lg border text-center\" style=\"background-color: var(--bg-secondary); border-color: var(--border);\"><div class=\"text-4xl mb-2\">📋</div><h3 class=\"text-lg font-semibold mb-2\" style=\"color: var(--text-primary)\">No Rules Yet</h3><p style=\"color: var(--text-secondary)\">Create auto-assign rules to automatically add books to this collection</p></div></div></div><div class=\"mb-8\"><h2 class=\"text-2xl font-bold mb-4\" style=\"color: var(--text-primary)\">Create New Rule</h2><form id=\"rule-form\" onsubmit=\"handleCreateRule(event)\"><input type=\"hidden\" id=\"collection-id\" value=\"{ collection.ID }\"><div class=\"grid grid-cols-1 md:grid-cols-2 gap-6 mb-6\"><div><label class=\"block text-sm font-medium mb-2\" style=\"color: var(--text-secondary)\">Field</label> <select id=\"rule-field\" required class=\"w-full px-4 py-2 border rounded-lg\" style=\"background-color: var(--bg-primary); color: var(--text-primary); border-color: var(--border);\"><option value=\"\">Select field...</option> <option value=\"genre\">Genre</option> <option value=\"series\">Series</option> <option value=\"author\">Author</option> <option value=\"language\">Language</option> <option value=\"publisher\">Publisher</option> <option value=\"copyright_year\">Copyright Year</option> <option value=\"tags\">Tags</option></select></div><div><label class=\"block text-sm font-medium mb-2\" style=\"color: var(--text-secondary)\">Operator</label> <select id=\"rule-operator\" required class=\"w-full px-4 py-2 border rounded-lg\" style=\"background-color: var(--bg-primary); color: var(--text-primary); border-color: var(--border);\"><option value=\"\">Select operator...</option> <option value=\"equals\">equals</option> <option value=\"not_equals\">does not equal</option> <option value=\"contains\">contains</option> <option value=\"not_contains\">does not contain</option> <option value=\"starts_with\">starts with</option> <option value=\"ends_with\">ends with</option> <option value=\"greater_than\">greater than</option> <option value=\"less_than\">less than</option></select></div></div><div class=\"mb-6\"><label class=\"block text-sm font-medium mb-2\" style=\"color: var(--text-secondary)\">Value</label> <input type=\"text\" id=\"rule-value\" required class=\"w-full px-4 py-2 border rounded-lg\" style=\"background-color: var(--bg-primary); color: var(--text-primary); border-color: var(--border);\" placeholder=\"e.g., Science Fiction\"></div><div class=\"mb-6\"><label class=\"flex items-center space-x-3 cursor-pointer\"><input type=\"checkbox\" id=\"rule-enabled\" checked class=\"w-5 h-5 rounded\"> <span style=\"color: var(--text-primary)\">Enable Rule</span></label><p class=\"text-xs mt-1\" style=\"color: var(--text-secondary)\">Uncheck to disable without deleting</p></div><div class=\"mb-6\"><label class=\"block text-sm font-medium mb-2\" style=\"color: var(--text-secondary)\">Priority</label><div class=\"flex gap-4\"><label class=\"flex items-center space-x-2 cursor-pointer\"><input type=\"radio\" name=\"priority\" value=\"1\" class=\"w-4 h-4\"> <span style=\"color: var(--text-primary)\">High</span></label> <label class=\"flex items-center space-x-2 cursor-pointer\"><input type=\"radio\" name=\"priority\" value=\"2\" checked class=\"w-4 h-4\"> <span style=\"color: var(--text-primary)\">Medium</span></label> <label class=\"flex items-center space-x-2 cursor-pointer\"><input type=\"radio\" name=\"priority\" value=\"3\" class=\"w-4 h-4\"> <span style=\"color: var(--text-primary)\">Low</span></label></div></div><div class=\"flex justify-end space-x-3\"><button type=\"button\" onclick=\"testRule()\" class=\"btn-secondary px-4 py-2 rounded-lg\">🧪 Test Rule</button> <button type=\"button\" onclick=\"clearForm()\" class=\"btn-secondary px-4 py-2 rounded-lg\">Clear</button> <button type=\"submit\" class=\"btn-primary px-4 py-2 rounded-lg\">➕ Add Rule</button></div></form><div id=\"test-results\" class=\"hidden card p-6 rounded-lg border mb-6\" style=\"background-color: var(--bg-primary); border-color: var(--border);\"><h3 class=\"font-semibold mb-3\" style=\"color: var(--text-primary)\">Rule Test Results</h3><p class=\"text-sm mb-2\" style=\"color: var(--text-secondary)\">Books that would be added by this rule:</p><div id=\"test-results-list\" class=\"max-h-64 overflow-y-auto space-y-2\"></div></div></div><div class=\"card p-6 rounded-lg border\" style=\"background-color: var(--bg-primary); border-color: var(--border);\"><h2 class=\"text-xl font-bold mb-4\" style=\"color: var(--text-primary)\">Rule Examples</h2><div class=\"space-y-4\"><div class=\"flex items-start gap-4\"><div class=\"flex-shrink-0 w-8 h-8 rounded-full flex items-center justify-center font-bold\" style=\"background-color: var(--accent);\">1</div><div><p class=\"font-medium\" style=\"color: var(--text-primary)\">Add all Science Fiction books</p><code class=\"block mt-1 text-sm\" style=\"color: var(--text-secondary); background-color: var(--bg-secondary); padding: 4px 8px; border-radius: 4px;\">Field: genre<br>Operator: equals<br>Value: Science Fiction</code></div></div><div class=\"flex items-start gap-4\"><div class=\"flex-shrink-0 w-8 h-8 rounded-full flex items-center justify-center font-bold\" style=\"background-color: var(--accent);\">2</div><div><p class=\"font-medium\" style=\"color: var(--text-primary)\">Add books from a specific series</p><code class=\"block mt-1 text-sm\" style=\"color: var(--text-secondary); background-color: var(--bg-secondary); padding: 4px 8px; border-radius: 4px;\">Field: series<br>Operator: starts with<br>Value: Harry Potter</code></div></div><div class=\"flex items-start gap-4\"><div class=\"flex-shrink-0 w-8 h-8 rounded-full flex items-center justify-center font-bold\" style=\"background-color: var(--accent);\">3</div><div><p class=\"font-medium\" style=\"color: var(--text-primary)\">Add books published in a year range</p><code class=\"block mt-1 text-sm\" style=\"color: var(--text-secondary); background-color: var(--bg-secondary); padding: 4px 8px; border-radius: 4px;\">Field: copyright_year<br>Operator: greater than<br>Value: 2020</code></div></div></div></div></div><script>\n            let collectionId = '{ collection.ID }';\n\n            function backToCollection() {\n                window.location.href = `/collections/${collectionId}`;\n            }\n\n            function renderRules(rules) {\n                const container = document.getElementById('rules-container');\n                const noRulesDiv = document.getElementById('no-rules');\n\n                if (rules && rules.length > 0) {\n                    noRulesDiv.classList.add('hidden');\n\n                    container.innerHTML = rules.map((rule, index) => `\n                        <div class=\"card p-4 rounded-lg border\" style=\"background-color: var(--bg-secondary); border-color: var(--border);\">\n                            <div class=\"flex justify-between items-start\">\n                                <div class=\"flex-1\">\n                                    <div class=\"flex items-center gap-3 mb-2\">\n                                        <span class=\"font-semibold\" style=\"color: var(--text-primary)\">\n                                            ${getFieldLabel(rule.field)}\n                                        </span>\n                                        <span class=\"px-2 py-1 text-xs rounded\" style=\"background-color: ${rule.enabled ? 'var(--accent)' : 'var(--text-secondary)'}; color: var(--bg-primary);\">\n                                            ${rule.enabled ? 'Enabled' : 'Disabled'}\n                                        </span>\n                                        <span class=\"px-2 py-1 text-xs rounded\" style=\"background-color: var(--bg-secondary); color: var(--text-primary);\">\n                                            Priority ${rule.priority}\n                                        </span>\n                                    </div>\n                                    <code class=\"block text-sm\" style=\"color: var(--text-secondary);\">\n                                        ${getOperatorLabel(rule.operator)} \"${rule.value}\"\n                                    </code>\n                                </div>\n                                <div class=\"flex space-x-2\">\n                                    <button onclick=\"toggleRule('${rule.id}', ${!rule.enabled})\"\n                                            class=\"p-2 hover:opacity-80 rounded\" style=\"color: var(--text-secondary); background-color: var(--bg-primary);\">\n                                        ${rule.enabled ? '⏸️' : '▶️'}\n                                    </button>\n                                    <button onclick=\"deleteRule('${rule.id}')\"\n                                            class=\"p-2 hover:opacity-80 rounded\" style=\"color: var(--text-secondary); background-color: var(--bg-primary);\">\n                                        🗑️\n                                    </button>\n                                </div>\n                            </div>\n                        </div>\n                    `).join('');\n                } else {\n                    noRulesDiv.classList.remove('hidden');\n                }\n            }\n\n            function getFieldLabel(field) {\n                const labels = {\n                    'genre': 'Genre',\n                    'series': 'Series',\n                    'author': 'Author',\n                    'language': 'Language',\n                    'publisher': 'Publisher',\n                    'copyright_year': 'Copyright Year',\n                    'tags': 'Tags'\n                };\n                return labels[field] || field;\n            }\n\n            function getOperatorLabel(operator) {\n                const labels = {\n                    'equals': 'equals',\n                    'not_equals': 'does not equal',\n                    'contains': 'contains',\n                    'not_contains': 'does not contain',\n                    'starts_with': 'starts with',\n                    'ends_with': 'ends with',\n                    'greater_than': 'greater than',\n                    'less_than': 'less than'\n                };\n                return labels[operator] || operator;\n            }\n\n            function handleCreateRule(event) {\n                event.preventDefault();\n\n                const data = {\n                    field: document.getElementById('rule-field').value,\n                    operator: document.getElementById('rule-operator').value,\n                    value: document.getElementById('rule-value').value,\n                    enabled: document.getElementById('rule-enabled').checked,\n                    priority: parseInt(document.querySelector('input[name=\"priority\"]:checked').value)\n                };\n\n                fetch(`/api/collections/${collectionId}/rules`, {\n                    method: 'POST',\n                    headers: {\n                        'Content-Type': 'application/json',\n                        'Authorization': 'Bearer ' + localStorage.getItem('token')\n                    },\n                    body: JSON.stringify(data)\n                })\n                .then(response => response.json())\n                .then(result => {\n                    showToast('Rule created successfully', 'success');\n                    clearForm();\n                    loadRules();\n                })\n                .catch(error => {\n                    showToast('Failed to create rule', 'error');\n                });\n            }\n\n            function testRule() {\n                const field = document.getElementById('rule-field').value;\n                const operator = document.getElementById('rule-operator').value;\n                const value = document.getElementById('rule-value').value;\n\n                if (!field || !operator || !value) {\n                    showToast('Please fill in all rule fields', 'error');\n                    return;\n                }\n\n                const testResultsDiv = document.getElementById('test-results');\n                const resultsList = document.getElementById('test-results-list');\n\n                testResultsDiv.classList.remove('hidden');\n                resultsList.innerHTML = '<p class=\"text-sm\" style=\"color: var(--text-secondary)\">Testing rule...</p>';\n\n                const rules = [{\n                    field: field,\n                    operator: operator,\n                    value: value\n                }];\n\n                fetch('/api/collections/test-rules', {\n                    method: 'POST',\n                    headers: {\n                        'Content-Type': 'application/json',\n                        'Authorization': 'Bearer ' + localStorage.getItem('token')\n                    },\n                    body: JSON.stringify({ rules: rules })\n                })\n                .then(response => response.json())\n                .then(result => {\n                    if (result.matches && result.matches.length > 0) {\n                        let html = '<p style=\"color: var(--text-secondary); font-size: 0.875rem;\">Found ' + result.matches.length + ' matching books:</p>';\n                        html += '<div style=\"display: flex; flex-direction: column; gap: 0.5rem;\">';\n\n                        result.matches.slice(0, 20).forEach(book => {\n                            html += '<div style=\"display: flex; align-items: center; gap: 0.75rem; padding: 0.5rem; border-radius: 0.25rem; background-color: var(--bg-secondary);\">';\n                            html += '<img src=\"' + (book.cover_image_path || '/static/placeholder-book.svg') + '\" alt=\"Cover\" style=\"width: 2rem; height: 3rem; object-fit: cover; border-radius: 0.25rem;\">';\n                            html += '<div style=\"flex: 1;\">';\n                            html += '<div style=\"font-size: 0.875rem; font-weight: 500; color: var(--text-primary);\">' + book.title + '</div>';\n                            if (book.author) {\n                                html += '<div style=\"font-size: 0.75rem; color: var(--text-secondary);\">' + book.author + '</div>';\n                            }\n                            html += '<div style=\"font-size: 0.75rem; color: var(--accent);\">' + book.match_reason + '</div>';\n                            html += '</div>';\n                            html += '</div>';\n                        });\n\n                        if (result.matches.length > 20) {\n                            html += '<p style=\"color: var(--text-secondary); font-size: 0.875rem;\">...and ' + (result.matches.length - 20) + ' more</p>';\n                        }\n\n                        html += '</div>';\n                        resultsList.innerHTML = html;\n                    } else {\n                        resultsList.innerHTML = '<p style=\"color: var(--text-secondary); font-size: 0.875rem;\">No books match this rule</p>';\n                    }\n                })\n                .catch(error => {\n                    resultsList.innerHTML = '<p class=\"text-sm\" style=\"color: var(--error)\">Failed to test rule</p>';\n                });\n            }\n\n            function toggleRule(ruleId, enabled) {\n                fetch(`/api/collections/${collectionId}/rules/${ruleId}`, {\n                    method: 'PUT',\n                    headers: {\n                        'Content-Type': 'application/json',\n                        'Authorization': 'Bearer ' + localStorage.getItem('token')\n                    },\n                    body: JSON.stringify({ enabled })\n                })\n                .then(response => response.json())\n                .then(result => {\n                    showToast('Rule updated', 'success');\n                    loadRules();\n                })\n                .catch(error => {\n                    showToast('Failed to update rule', 'error');\n                });\n            }\n\n            function deleteRule(ruleId) {\n                if (!confirm('Are you sure you want to delete this rule?')) return;\n\n                fetch(`/api/collections/${collectionId}/rules/${ruleId}`, {\n                    method: 'DELETE',\n                    headers: { 'Authorization': 'Bearer ' + localStorage.getItem('token') }\n                })\n                .then(response => {\n                    if (response.ok) {\n                        showToast('Rule deleted', 'success');\n                        loadRules();\n                    } else {\n                        showToast('Failed to delete rule', 'error');\n                    }\n                })\n                .catch(error => {\n                    showToast('Failed to delete rule', 'error');\n                });\n            }\n\n            function clearForm() {\n                document.getElementById('rule-field').value = '';\n                document.getElementById('rule-operator').value = '';\n                document.getElementById('rule-value').value = '';\n                document.getElementById('rule-enabled').checked = true;\n                document.querySelector('input[name=\"priority\"][value=\"2\"]').checked = true;\n                document.getElementById('test-results').classList.add('hidden');\n            }\n\n            function loadRules() {\n                fetch(`/api/collections/${collectionId}`)\n                .then(response => response.json())\n                .then(result => {\n                    renderRules(result.auto_assign_rules || []);\n                })\n                .catch(error => {\n                    console.error('Failed to load rules', error);\n                });\n            }\n\n            function logout() {\n                localStorage.removeItem('token');\n                window.location.href = '/login';\n            }\n\n            // Load rules on page load\n            loadRules();\n        </script></body></html>")
		if templ_7745c5c3_Err != nil {
			return templ_7745c5c3_Err
		}
		return nil
	})
}

var _ = templruntime.GeneratedTemplate
