<?php

namespace App\Filament\Tenant\Resources;

use App\Filament\Tenant\Resources\MarketingChannelResource\Pages;
use App\Models\Tenant\MarketingChannel;
use Filament\Actions;
use Filament\Forms;
use Filament\Resources\Resource;
use Filament\Schemas;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;

class MarketingChannelResource extends Resource
{
    protected static ?string $model = MarketingChannel::class;

    protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-megaphone';

    protected static ?string $navigationLabel = 'Canale marketing';

    protected static string|\UnitEnum|null $navigationGroup = 'Marketing';

    protected static ?string $modelLabel = 'canal';

    protected static ?string $pluralModelLabel = 'canale marketing';

    protected static ?int $navigationSort = 61;

    public static function form(Schema $schema): Schema
    {
        return $schema->components([
            Schemas\Components\Section::make('Identificare')
                ->columns(3)
                ->schema([
                    Forms\Components\TextInput::make('name')->label('Nume canal')->required()->maxLength(120),
                    Forms\Components\TextInput::make('icon')->label('Iconiță (emoji)')->maxLength(8)->placeholder('🔍 / 📘 / 📸'),
                    Forms\Components\ColorPicker::make('color'),
                ]),
            Schemas\Components\Section::make('Buget & rezultate (luna curentă)')
                ->columns(3)
                ->schema([
                    Forms\Components\TextInput::make('budget_monthly')->label('Buget')->numeric()->default(0),
                    Forms\Components\TextInput::make('spent_monthly')->label('Cheltuit')->numeric()->default(0),
                    Forms\Components\TextInput::make('revenue')->label('Venit generat')->numeric()->default(0),
                    Forms\Components\TextInput::make('leads_count')->label('Lead-uri')->numeric()->default(0),
                    Forms\Components\TextInput::make('converted_count')->label('Convertite')->numeric()->default(0),
                    Forms\Components\Toggle::make('is_active')->label('Activ')->default(true),
                ]),
            Forms\Components\Textarea::make('notes')->label('Observații')->columnSpanFull()->rows(2),
        ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\ColorColumn::make('color')->label(''),
                Tables\Columns\TextColumn::make('icon')->label('')->width(40),
                Tables\Columns\TextColumn::make('name')->searchable()->sortable(),
                Tables\Columns\TextColumn::make('budget_monthly')->label('Buget')->money('MDL')->alignRight(),
                Tables\Columns\TextColumn::make('spent_monthly')->label('Cheltuit')->money('MDL')->alignRight(),
                Tables\Columns\TextColumn::make('leads_count')->label('Lead-uri')->alignRight(),
                Tables\Columns\TextColumn::make('converted_count')->label('Convertite')->alignRight(),
                Tables\Columns\TextColumn::make('revenue')->label('Venit')->money('MDL')->alignRight()->color('success'),
                Tables\Columns\TextColumn::make('roi')
                    ->label('ROI')
                    ->state(fn (MarketingChannel $r) => $r->roi)
                    ->formatStateUsing(fn ($s) => $s . '%')
                    ->color(fn ($s) => $s >= 0 ? 'success' : 'danger')
                    ->alignRight(),
                Tables\Columns\TextColumn::make('cost_per_lead')
                    ->label('Cost/lead')
                    ->state(fn (MarketingChannel $r) => $r->cost_per_lead)
                    ->money('MDL')
                    ->alignRight(),
                Tables\Columns\IconColumn::make('is_active')->boolean(),
            ])
            ->filters([
                Tables\Filters\TernaryFilter::make('is_active')->label('Active'),
            ])
            ->actions([
                Actions\EditAction::make(),
                Actions\DeleteAction::make(),
            ])
            ->defaultSort('revenue', 'desc');
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListMarketingChannels::route('/'),
            'create' => Pages\CreateMarketingChannel::route('/create'),
            'edit' => Pages\EditMarketingChannel::route('/{record}/edit'),
        ];
    }
}
