@page "/design/templates/create"
@using ScadaLink.Security
@using ScadaLink.Commons.Entities.Templates
@using ScadaLink.Commons.Interfaces.Repositories
@using ScadaLink.TemplateEngine
@attribute [Authorize(Policy = AuthorizationPolicies.RequireDesign)]
@inject ITemplateEngineRepository TemplateEngineRepository
@inject TemplateService TemplateService
@inject AuthenticationStateProvider AuthStateProvider
@inject NavigationManager NavigationManager

<div class="container-fluid mt-3">
    <div class="mb-3">
        <button class="btn btn-outline-secondary btn-sm"
                aria-label="Back to Templates"
                @onclick="GoBack">← Back</button>
    </div>

    <h4 class="mb-3">Create Template</h4>

    @if (_loading)
    {
        <LoadingSpinner IsLoading="true" />
    }
    else
    {
        <div class="card">
            <div class="card-body">
                <div class="mb-3">
                    <label class="form-label">Name</label>
                    <input type="text" class="form-control" @bind="_createName" />
                </div>
                <div class="mb-3">
                    <label class="form-label">Parent Template</label>
                    <select class="form-select" @bind="_createParentId">
                        <option value="0">(None - root template)</option>
                        @foreach (var t in _templates)
                        {
                            <option value="@t.Id">@t.Name</option>
                        }
                    </select>
                </div>
                <div class="mb-3">
                    <label class="form-label">Description</label>
                    <input type="text" class="form-control" @bind="_createDescription" />
                </div>
                @if (_formError != null)
                {
                    <div class="text-danger small mt-2">@_formError</div>
                }
                <div class="mt-3">
                    <button class="btn btn-success me-1" @onclick="CreateTemplate">Create</button>
                    <button class="btn btn-outline-secondary" @onclick="GoBack">Cancel</button>
                </div>
            </div>
        </div>
    }
</div>

@code {
    [SupplyParameterFromQuery] public int? FolderId { get; set; }
    [SupplyParameterFromQuery] public int? ParentId { get; set; }

    private List<Template> _templates = new();
    private bool _loading = true;

    private string _createName = string.Empty;
    private int _createParentId;
    private string? _createDescription;
    private string? _formError;

    protected override async Task OnInitializedAsync()
    {
        try
        {
            _templates = (await TemplateEngineRepository.GetAllTemplatesAsync()).ToList();
            if (ParentId is int pid && _templates.Any(t => t.Id == pid))
            {
                _createParentId = pid;
            }
        }
        catch (Exception ex)
        {
            _formError = $"Failed to load templates: {ex.Message}";
        }
        _loading = false;
    }

    private async Task CreateTemplate()
    {
        _formError = null;
        if (string.IsNullOrWhiteSpace(_createName)) { _formError = "Name is required."; return; }

        try
        {
            var user = await GetCurrentUserAsync();
            var result = await TemplateService.CreateTemplateAsync(
                _createName.Trim(), _createDescription?.Trim(),
                _createParentId == 0 ? null : _createParentId, user,
                folderId: FolderId);

            if (result.IsSuccess)
            {
                NavigationManager.NavigateTo($"/design/templates/{result.Value.Id}");
            }
            else
            {
                _formError = result.Error;
            }
        }
        catch (Exception ex)
        {
            _formError = ex.Message;
        }
    }

    private void GoBack()
    {
        NavigationManager.NavigateTo("/design/templates");
    }

    private async Task<string> GetCurrentUserAsync()
    {
        var authState = await AuthStateProvider.GetAuthenticationStateAsync();
        return authState.User.FindFirst("Username")?.Value ?? "unknown";
    }
}
