<?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';

    public static function getNavigationLabel(): string
    {
        return __('nav.label.Canale marketing');
    }

    public static function getNavigationGroup(): ?string
    {
        return __('nav.group.Marketing');
    }

    protected static ?string $modelLabel = null;

    public static function getModelLabel(): string
    {
        return __('canal');
    }

    protected static ?string $pluralModelLabel = null;

    public static function getPluralModelLabel(): string
    {
        return __('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'),
        ];
    }
}
