<?php

namespace App\Filament\Pages;

use App\Models\Project;
use Filament\Pages\Page;
use Filament\Tables\Table;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Concerns\InteractsWithTable;
use Filament\Tables\Contracts\HasTable;

class ProjectReport extends Page implements HasTable
{
    use InteractsWithTable;

    protected static string | \BackedEnum | null $navigationIcon = 'heroicon-o-chart-bar';
    protected static ?string $navigationLabel = 'Báo cáo theo Dự án';
    protected static ?string $title = 'Báo cáo Thống kê theo Dự án';
    protected static string | \UnitEnum | null $navigationGroup = 'Quản lý Dòng tiền';
    protected static ?int $navigationSort = 50;
    protected string $view = 'filament.pages.project-report';

    public function table(Table $table): Table
    {
        return $table
            ->query(
                Project::query()
                    ->select('projects.id', 'projects.name', 'projects.code')
                    ->selectRaw('COUNT(DISTINCT products.id) as product_count')
                    ->selectRaw('COUNT(DISTINCT CASE WHEN contracts.id IS NOT NULL THEN products.id END) as sold_product_count')
                    ->selectRaw('COUNT(DISTINCT contracts.id) as contract_count')
                    ->selectRaw('COALESCE(SUM(contracts.total_value), 0) as total_revenue')
                    ->selectRaw('COALESCE(SUM(contracts.paid_amount), 0) as total_paid')
                    ->selectRaw('COALESCE(SUM(contracts.remaining_amount), 0) as total_remaining')
                    ->leftJoin('products', 'products.project_id', '=', 'projects.id')
                    ->leftJoin('contracts', 'contracts.product_id', '=', 'products.id')
                    ->groupBy('projects.id', 'projects.name', 'projects.code')
            )
            ->columns([
                TextColumn::make('name')
                    ->label('Dự án')
                    ->searchable()
                    ->sortable(),

                TextColumn::make('product_count')
                    ->label('Tổng SP')
                    ->alignCenter()
                    ->sortable(),

                TextColumn::make('sold_product_count')
                    ->label('Đã bán')
                    ->alignCenter()
                    ->sortable()
                    ->color('success'),

                TextColumn::make('contract_count')
                    ->label('Số HĐ')
                    ->alignCenter()
                    ->sortable(),

                TextColumn::make('total_revenue')
                    ->label('Tổng giá trị HĐ')
                    ->money('VND')
                    ->sortable()
                    ->summarize(\Filament\Tables\Columns\Summarizers\Sum::make()->label('Tổng')->money('VND')),

                TextColumn::make('total_paid')
                    ->label('Đã thu')
                    ->money('VND')
                    ->sortable()
                    ->color('success')
                    ->summarize(\Filament\Tables\Columns\Summarizers\Sum::make()->label('Tổng')->money('VND')),

                TextColumn::make('total_remaining')
                    ->label('Công nợ phải thu')
                    ->money('VND')
                    ->sortable()
                    ->color('danger')
                    ->summarize(\Filament\Tables\Columns\Summarizers\Sum::make()->label('Tổng')->money('VND')),
            ])
            ->defaultSort('total_revenue', 'desc')
            ->paginated([10, 25, 50]);
    }
}
