<?php

namespace App\Filament\Resources;

use App\Filament\Resources\MfmReadingResource\Pages;
use App\Filament\Resources\MfmReadingResource\RelationManagers;
use App\Models\MfmReading;
use App\Models\Plant;
use Filament\Facades\Filament;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;

class MfmReadingResource extends Resource
{
    protected static ?string $model = MfmReading::class;

    protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

    protected static ?string $navigationGroup = 'Power House';

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\Select::make('plant_id')
                    ->relationship('plant', 'name')
                    ->options(function (callable $get) {
                        $userHas = Filament::auth()->user()->plant_id;
                        return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::pluck('name', 'id')->toArray();
                    })
                    ->required(),
                Forms\Components\Select::make('mfm_meter_id')
                    ->relationship('mfmMeter', 'name')
                    ->required(),
                Forms\Components\TextInput::make('apparent_energy_received'),
                Forms\Components\TextInput::make('reactive_energy_received'),
                Forms\Components\TextInput::make('active_energy_received'),
                Forms\Components\TextInput::make('active_power_r'),
                Forms\Components\TextInput::make('active_power_y'),
                Forms\Components\TextInput::make('active_power_b'),
                Forms\Components\TextInput::make('active_power_total'),
                Forms\Components\TextInput::make('voltage_ry'),
                Forms\Components\TextInput::make('voltage_yb'),
                Forms\Components\TextInput::make('voltage_br'),
                Forms\Components\TextInput::make('current_r'),
                Forms\Components\TextInput::make('current_y'),
                Forms\Components\TextInput::make('current_b'),
                Forms\Components\TextInput::make('current_n'),
                Forms\Components\TextInput::make('voltage_r_n'),
                Forms\Components\TextInput::make('voltage_y_n'),
                Forms\Components\TextInput::make('voltage_b_n'),
                Forms\Components\TextInput::make('frequency'),
                Forms\Components\TextInput::make('power_factor_r'),
                Forms\Components\TextInput::make('power_factor_y'),
                Forms\Components\TextInput::make('power_factor_b'),
                Forms\Components\TextInput::make('power_factor_total'),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('No.')
                    ->label('No.')
                    ->getStateUsing(function ($record, $livewire, $column, $rowLoop) {
                        $paginator = $livewire->getTableRecords();
                        $perPage = method_exists($paginator, 'perPage') ? $paginator->perPage() : 10;
                        $currentPage = method_exists($paginator, 'currentPage') ? $paginator->currentPage() : 1;
                        return ($currentPage - 1) * $perPage + $rowLoop->iteration;
                    }),
                Tables\Columns\TextColumn::make('plant.name')
                    ->label('Plant')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('mfmMeter.name')
                    ->label('Mfm Meter')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('apparent_energy_received')
                    ->label('Apparent Energy Received')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('reactive_energy_received')
                    ->label('Reactive Energy Received')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('active_energy_received')
                    ->label('Active Energy Received')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('active_power_r')
                    ->label('Active Power R')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('active_power_y')
                    ->label('Active Power Y')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('active_power_b')
                    ->label('Active Power B')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('active_power_total')
                    ->label('Active Power Total')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('voltage_ry')
                    ->label('Voltage RY')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('voltage_yb')
                    ->label('Voltage YB')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('voltage_br')
                    ->label('Voltage BR')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('current_r')
                    ->label('Current R')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('current_y')
                  ->label('Current Y')
                  ->alignCenter()
                  ->sortable(),
                Tables\Columns\TextColumn::make('current_b')
                    ->label('Current B')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('current_n')
                    ->label('Current N')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('voltage_r_n')
                    ->label('Voltage R N')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('voltage_y_n')
                    ->label('Voltage Y N')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('voltage_b_n')
                    ->label('Voltage B N')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('frequency')
                    ->label('Frequency')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('power_factor_r')
                    ->label('Power Factor R')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('power_factor_y')
                    ->label('Power Factor Y')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('power_factor_b')
                    ->label('Power Factor B')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('power_factor_total')
                    ->label('Power Factor Total')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('created_at')
                    ->label('Created At')
                    ->alignCenter()
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                Tables\Columns\TextColumn::make('updated_at')
                    ->label('Updated At')
                    ->alignCenter()
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                Tables\Columns\TextColumn::make('deleted_at')
                    ->label('Deleted At')
                    ->alignCenter()
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                Tables\Filters\TrashedFilter::make(),
            ])
            ->actions([
                Tables\Actions\ViewAction::make(),
                Tables\Actions\EditAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                    Tables\Actions\ForceDeleteBulkAction::make(),
                    Tables\Actions\RestoreBulkAction::make(),
                ]),
            ]);
    }

    public static function getRelations(): array
    {
        return [
            //
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListMfmReadings::route('/'),
            'create' => Pages\CreateMfmReading::route('/create'),
            'view' => Pages\ViewMfmReading::route('/{record}'),
            'edit' => Pages\EditMfmReading::route('/{record}/edit'),
        ];
    }

    public static function getEloquentQuery(): Builder
    {
        return parent::getEloquentQuery()
            ->withoutGlobalScopes([
                SoftDeletingScope::class,
            ]);
    }
}
