@page "/portfolio"
@attribute [Authorize]
@inject HttpClient Http

<PageTitle>QuantEngine - 포트폴리오</PageTitle>

<!-- Page Header -->
<div class="mb-6">
    <MudText Typo="Typo.h4" Class="mb-2">포트폴리오</MudText>
    <MudText Typo="Typo.body1" Class="text-muted">자산 구성 및 성과 분석</MudText>
</div>

<!-- Summary Cards -->
<MudGrid Spacing="3" Class="mb-6">
    <MudItem xs="12" sm="6" md="3">
        <MudPaper Class="pa-4" Elevation="0" Style="border: 1px solid var(--mud-palette-divider);">
            <MudText Typo="Typo.caption" Class="text-muted mb-1">총 평가액</MudText>
            <MudText Typo="Typo.h5" Class="text-primary">₩125.5M</MudText>
            <MudText Typo="Typo.body2" Class="text-success mt-1">+3.2% (이번 달)</MudText>
        </MudPaper>
    </MudItem>

    <MudItem xs="12" sm="6" md="3">
        <MudPaper Class="pa-4" Elevation="0" Style="border: 1px solid var(--mud-palette-divider);">
            <MudText Typo="Typo.caption" Class="text-muted mb-1">보유 종목</MudText>
            <MudText Typo="Typo.h5">12개</MudText>
            <MudText Typo="Typo.body2" Class="text-muted mt-1">주식 및 펀드</MudText>
        </MudPaper>
    </MudItem>

    <MudItem xs="12" sm="6" md="3">
        <MudPaper Class="pa-4" Elevation="0" Style="border: 1px solid var(--mud-palette-divider);">
            <MudText Typo="Typo.caption" Class="text-muted mb-1">수익률</MudText>
            <MudText Typo="Typo.h5" Class="text-success">+8.5%</MudText>
            <MudText Typo="Typo.body2" Class="text-muted mt-1">연간 기준</MudText>
        </MudPaper>
    </MudItem>

    <MudItem xs="12" sm="6" md="3">
        <MudPaper Class="pa-4" Elevation="0" Style="border: 1px solid var(--mud-palette-divider);">
            <MudText Typo="Typo.caption" Class="text-muted mb-1">위험도</MudText>
            <MudText Typo="Typo.h5">중간</MudText>
            <MudChip T="string" Label="true" Size="Size.Small" Color="Color.Warning" Variant="Variant.Filled" Class="mt-1">
                Moderate
            </MudChip>
        </MudPaper>
    </MudItem>
</MudGrid>

<!-- Asset Breakdown -->
<MudGrid Spacing="3" Class="mb-6">
    <MudItem xs="12" md="8">
        <MudPaper Class="pa-4" Elevation="1">
            <MudText Typo="Typo.h6" Class="mb-4">자산 구성</MudText>

            <MudTable Items="@_assets" Dense="true" Hover="true" Striped="true">
                <HeaderContent>
                    <MudTh>종목/펀드명</MudTh>
                    <MudTh>수량</MudTh>
                    <MudTh>현재가</MudTh>
                    <MudTh>평가액</MudTh>
                    <MudTh>수익률</MudTh>
                    <MudTh>비율</MudTh>
                </HeaderContent>
                <RowTemplate>
                    <MudTd DataLabel="Name">
                        <div class="d-flex align-items-center gap-2">
                            <MudAvatar Size="Size.Small" Color="Color.Primary">@context.Name[0]</MudAvatar>
                            <div>
                                <MudText Typo="Typo.body2" Class="font-weight-500">@context.Name</MudText>
                                <MudText Typo="Typo.caption" Class="text-muted">@context.Ticker</MudText>
                            </div>
                        </div>
                    </MudTd>
                    <MudTd DataLabel="Quantity">
                        <MudText Typo="Typo.body2">@context.Quantity.ToString("N0")</MudText>
                    </MudTd>
                    <MudTd DataLabel="Price">
                        <MudText Typo="Typo.body2">₩@context.CurrentPrice.ToString("N0")</MudText>
                    </MudTd>
                    <MudTd DataLabel="Value">
                        <MudText Typo="Typo.body2" Class="font-weight-500">₩@context.Value.ToString("N0")</MudText>
                    </MudTd>
                    <MudTd DataLabel="Return">
                        <MudChip T="string" Label="true" Size="Size.Small"
                                 Color="@(context.ReturnRate >= 0 ? Color.Success : Color.Error)"
                                 Variant="Variant.Filled">
                            @(context.ReturnRate >= 0 ? "+" : "")@context.ReturnRate.ToString("F1")%
                        </MudChip>
                    </MudTd>
                    <MudTd DataLabel="Ratio">
                        <MudText Typo="Typo.body2">@context.Ratio.ToString("F1")%</MudText>
                    </MudTd>
                </RowTemplate>
            </MudTable>
        </MudPaper>
    </MudItem>

    <MudItem xs="12" md="4">
        <MudPaper Class="pa-4" Elevation="1">
            <MudText Typo="Typo.h6" Class="mb-4">자산 분류</MudText>

            <MudStack Spacing="2">
                @foreach (var category in AssetCategories)
                {
                    <div>
                        <div class="d-flex justify-content-between mb-1">
                            <MudText Typo="Typo.body2">@category.Name</MudText>
                            <MudText Typo="Typo.body2" Class="font-weight-500">@category.Percentage%</MudText>
                        </div>
                        <MudProgressLinear Value="@category.Percentage" Color="@category.Color" />
                    </div>
                }
            </MudStack>
        </MudPaper>
    </MudItem>
</MudGrid>

<!-- Trading History -->
<MudPaper Class="pa-4" Elevation="1">
    <MudText Typo="Typo.h6" Class="mb-4">거래 이력</MudText>

    @if (TradingHistory.Count == 0)
    {
        <MudAlert Severity="Severity.Info">거래 이력이 없습니다.</MudAlert>
    }
    else
    {
        <MudTable Items="@TradingHistory" Dense="true" Hover="true" Striped="true">
            <HeaderContent>
                <MudTh>일자</MudTh>
                <MudTh>종목</MudTh>
                <MudTh>구분</MudTh>
                <MudTh>수량</MudTh>
                <MudTh>단가</MudTh>
                <MudTh>금액</MudTh>
                <MudTh>수수료</MudTh>
            </HeaderContent>
            <RowTemplate>
                <MudTd DataLabel="Date">
                    <MudText Typo="Typo.body2">@context.Date.ToString("yyyy-MM-dd")</MudText>
                </MudTd>
                <MudTd DataLabel="Ticker">
                    <MudText Typo="Typo.body2">@context.Ticker</MudText>
                </MudTd>
                <MudTd DataLabel="Type">
                    <MudChip T="string" Label="true" Size="Size.Small"
                             Color="@(context.Type == "매수" ? Color.Success : Color.Error)"
                             Variant="Variant.Filled">
                        @context.Type
                    </MudChip>
                </MudTd>
                <MudTd DataLabel="Quantity">
                    <MudText Typo="Typo.body2">@context.Quantity</MudText>
                </MudTd>
                <MudTd DataLabel="Price">
                    <MudText Typo="Typo.body2">₩@context.Price.ToString("N0")</MudText>
                </MudTd>
                <MudTd DataLabel="Amount">
                    <MudText Typo="Typo.body2">₩@context.Amount.ToString("N0")</MudText>
                </MudTd>
                <MudTd DataLabel="Fee">
                    <MudText Typo="Typo.body2" Class="text-muted">₩@context.Fee.ToString("N0")</MudText>
                </MudTd>
            </RowTemplate>
        </MudTable>
    }
</MudPaper>

@code {
    private List<AssetModel> _assets = new();
    private List<CategoryModel> AssetCategories = new();
    private List<TradeModel> TradingHistory = new();

    protected override async Task OnInitializedAsync()
    {
        await LoadAssets();
    }

    private async Task LoadAssets()
    {
        _assets = new List<AssetModel>
        {
            new AssetModel { Name = "삼성전자", Ticker = "005930", Quantity = 50, CurrentPrice = 70000, Value = 3500000, ReturnRate = 5.2M, Ratio = 28.0M },
            new AssetModel { Name = "LG화학", Ticker = "051910", Quantity = 30, CurrentPrice = 820000, Value = 24600000, ReturnRate = -2.1M, Ratio = 19.6M },
            new AssetModel { Name = "현대차", Ticker = "005380", Quantity = 40, CurrentPrice = 245000, Value = 9800000, ReturnRate = 8.5M, Ratio = 7.8M },
            new AssetModel { Name = "SK하이닉스", Ticker = "000660", Quantity = 25, CurrentPrice = 105000, Value = 2625000, ReturnRate = 12.3M, Ratio = 2.1M },
            new AssetModel { Name = "삼성중공업", Ticker = "010140", Quantity = 60, CurrentPrice = 85000, Value = 5100000, ReturnRate = 3.7M, Ratio = 4.1M },
            new AssetModel { Name = "포스코", Ticker = "005490", Quantity = 20, CurrentPrice = 75000, Value = 1500000, ReturnRate = -5.2M, Ratio = 1.2M },
        };

        AssetCategories = new List<CategoryModel>
        {
            new CategoryModel { Name = "대형주", Percentage = 45, Color = Color.Primary },
            new CategoryModel { Name = "중형주", Percentage = 30, Color = Color.Secondary },
            new CategoryModel { Name = "소형주", Percentage = 15, Color = Color.Info },
            new CategoryModel { Name = "채권/현금", Percentage = 10, Color = Color.Success }
        };

        TradingHistory = new List<TradeModel>
        {
            new TradeModel { Date = DateTime.Now.AddDays(-5), Ticker = "005930", Type = "매수", Quantity = 10, Price = 68000, Amount = 680000, Fee = 1360 },
            new TradeModel { Date = DateTime.Now.AddDays(-10), Ticker = "051910", Type = "매도", Quantity = 5, Price = 850000, Amount = 4250000, Fee = 8500 },
            new TradeModel { Date = DateTime.Now.AddDays(-15), Ticker = "005380", Type = "매수", Quantity = 20, Price = 240000, Amount = 4800000, Fee = 9600 },
        };

        await Task.CompletedTask;
    }

    private class AssetModel
    {
        public string Name { get; set; }
        public string Ticker { get; set; }
        public int Quantity { get; set; }
        public decimal CurrentPrice { get; set; }
        public decimal Value { get; set; }
        public decimal ReturnRate { get; set; }
        public decimal Ratio { get; set; }
    }

    private class CategoryModel
    {
        public string Name { get; set; }
        public int Percentage { get; set; }
        public Color Color { get; set; }
    }

    private class TradeModel
    {
        public DateTime Date { get; set; }
        public string Ticker { get; set; }
        public string Type { get; set; }
        public int Quantity { get; set; }
        public decimal Price { get; set; }
        public decimal Amount { get; set; }
        public decimal Fee { get; set; }
    }
}
