<?php

namespace App\Livewire;

use App\Exports\ProductionPlanExport;
use App\Models\ProductionPlan;
use App\Models\ProductionQuantity;
use Livewire\Component;
use Carbon\Carbon;
use DB;
use Maatwebsite\Excel\Facades\Excel;

class ProductionTargetPlan extends Component
{

    public $plantId, $lineId, $month, $year;

    public $records = [];

    public $dates = [];

    public $leaveDates = [];

    public $productionPlanDates = '';


    protected $listeners = [
        'loadData' => 'loadProductionData',
        'loadCategoryData' => 'loadProductionDataCategory',
        'loadData1' => 'exportProductionData',
    ];

    public function getMonthDates($month, $year)
    {
        $start = Carbon::createFromDate($year, $month, 1);
        $days  = $start->daysInMonth;

        $dates = [];

        for ($i = 1; $i <= $days; $i++) {
            $dates[] = Carbon::createFromDate($year, $month, $i)
                ->format('Y-m-d');
        }

        return $dates;
    }

    // public function loadProductionData($plantId, $lineId, $month, $year){

    //     if (!$plantId || !$lineId || !$month || !$year) {
    //         $this->records = [];
    //         $this->dates = [];
    //         $this->leaveDates = [];
    //         return;
    //     }

    //     $this->dates = $this->getMonthDates($month, $year);

    //     $data = ProductionPlan::query()
    //     ->join('items', 'items.id', '=', 'production_plans.item_id')
    //     ->join('lines', 'lines.id', '=', 'production_plans.line_id')
    //     ->join('plants', 'plants.id', '=', 'production_plans.plant_id')
    //     ->where('production_plans.plant_id', $plantId)
    //     ->where('production_plans.line_id', $lineId)
    //     ->whereMonth('production_plans.created_at', $month)
    //     ->whereYear('production_plans.created_at', $year)
    //     ->select(
    //         'production_plans.created_at',
    //         'production_plans.operator_id',
    //         'plants.name as plant',
    //         'items.code as item_code',
    //         'items.description as item_description',
    //         'lines.name as line_name',
    //         'production_plans.leave_dates'
    //     )
    //     ->first();

    //     if ($data && $data->leave_dates) {
    //         $this->leaveDates = array_map('trim', explode(',', $data->leave_dates));
    //     }

    //     $producedData = ProductionQuantity::selectRaw("
    //         plant_id,
    //         line_id,
    //         item_id,
    //         DATE(created_at) as prod_date,
    //         COUNT(*) as total_qty
    //     ")
    //     ->where('plant_id', $plantId)
    //     ->where('line_id', $lineId)
    //     ->whereMonth('created_at', $month)
    //     ->whereYear('created_at', $year)
    //     ->groupBy('plant_id', 'line_id', 'item_id', DB::raw('DATE(created_at)'))
    //     ->get()
    //     ->groupBy(function ($row) {
    //         return $row->plant_id . '_' . $row->line_id . '_' . $row->item_id;
    //     })
    //     ->map(function ($group) {
    //         return $group->keyBy('prod_date');
    //     });

    //     $this->records = ProductionPlan::query()
    //         ->join('items', 'items.id', '=', 'production_plans.item_id')
    //         ->join('lines', 'lines.id', '=', 'production_plans.line_id')
    //         ->join('plants', 'plants.id', '=', 'production_plans.plant_id')
    //         ->where('production_plans.plant_id', $plantId)
    //         ->where('production_plans.line_id', $lineId)
    //         ->whereMonth('production_plans.created_at', $month)
    //         ->whereYear('production_plans.created_at', $year)
    //         ->select(
    //             'production_plans.item_id',
    //             'production_plans.plant_id',
    //             'production_plans.line_id',
    //             'production_plans.plan_quantity',
    //             'production_plans.working_days',
    //             'items.code as item_code',
    //             'items.description as item_description',
    //             'lines.name as line_name',
    //             'plants.name as plant_name'
    //         )
    //         ->get()
    //         ->map(function ($row) use ($producedData) {

    //             $row = $row->toArray();

    //             $remainingQty = $row['plan_quantity'];
    //             // $remainingDays = $row['working_days'];
    //             $remainingDays = (int) ($row['working_days'] ?? 0);

    //             $row['daily_target_dynamic'] = [];
    //             $row['produced_quantity'] = [];

    //             $key = $row['plant_id'].'_'.$row['line_id'].'_'.$row['item_id'];

    //             foreach ($this->dates as $date) {

    //                 // Skip leave dates
    //                 if (in_array($date, $this->leaveDates)) {
    //                     $row['daily_target_dynamic'][$date] = '-';
    //                     $row['produced_quantity'][$date] = '-';
    //                     continue;
    //                 }

    //                 $todayTarget = $remainingDays > 0
    //                     ? round($remainingQty / $remainingDays, 2)
    //                     : 0;

    //                 //$todayTarget = $remainingDays > 0
    //                 // ? $remainingQty / $remainingDays
    //                 // : 0;

    //                 $producedQty = isset($producedData[$key][$date])
    //                     ? $producedData[$key][$date]->total_qty
    //                     : 0;

    //                 $row['daily_target_dynamic'][$date] = $todayTarget;
    //                 $row['produced_quantity'][$date] = $producedQty;

    //                 // Carry forward pending
    //                 $remainingQty -= $producedQty;
    //                 if ($remainingQty < 0) {
    //                     $remainingQty = 0;
    //                 }

    //                 if ($remainingDays > 0) {
    //                     $remainingDays--;
    //                 }

    //                 // $remainingDays--;
    //             }

    //             return $row;
    //         })
    //         ->toArray();
    // }



    public function loadProductionData($year, $month, $plantId, $lineId, $category)
    {
        if (!$plantId || !$lineId || !$month || !$year) {
            $this->records = [];
            $this->dates = [];
            $this->leaveDates = [];
            return;
        }

        $dates = $this->getMonthDates($month, $year);
        $this->dates = $dates;

        $plans = ProductionPlan::query()
            ->join('items', 'items.id', '=', 'production_plans.item_id')
            ->join('lines', 'lines.id', '=', 'production_plans.line_id')
            ->join('plants', 'plants.id', '=', 'production_plans.plant_id')
            ->where('production_plans.plant_id', $plantId)
            ->where('production_plans.line_id', $lineId)
            ->whereMonth('production_plans.created_at', $month)
            ->whereYear('production_plans.created_at', $year)
            ->when($category, function ($query) use ($category) {
                $query->where('items.category', $category);
            })
            ->select(
                'production_plans.item_id',
                'production_plans.plant_id',
                'production_plans.line_id',
                'production_plans.plan_quantity',
                'production_plans.working_days',
                'production_plans.leave_dates',
                'items.code as item_code',
                'items.category as category',
                'items.description as item_description',
                'items.hourly_quantity as hourly_quantity',
                'lines.name as line_name',
                // 'lines.line_capacity as line_capacity',
                'plants.name as plant_name'
            )
            ->get();

        $leaveDates = [];

        if ($plans->isNotEmpty() && $plans[0]->leave_dates) {
            $leaveDates = array_map('trim', explode(',', $plans[0]->leave_dates));
        }

        $this->leaveDates = $leaveDates;

        $producedData = ProductionQuantity::selectRaw("
                plant_id,
                line_id,
                item_id,
                DATE(created_at) as prod_date,
                COUNT(*) as total_qty
            ")
            ->where('plant_id', $plantId)
            ->where('line_id', $lineId)
            ->whereMonth('created_at', $month)
            ->whereYear('created_at', $year)
            ->when($category, function ($query) use ($category) {
                $query->whereHas('item', function ($q) use ($category) {
                    $q->where('category', $category);
                });
            })
            ->groupBy('plant_id', 'line_id', 'item_id', DB::raw('DATE(created_at)'))
            ->get()
            ->groupBy(fn($row) =>
                $row->plant_id . '_' . $row->line_id . '_' . $row->item_id
            )
            ->map(fn($group) => $group->keyBy('prod_date'));


        $records = [];

        foreach ($plans as $plan) {

            $row = $plan->toArray();

            $remainingQty  = (float) $row['plan_quantity'];
            $remainingDays = (int) ($row['working_days'] ?? 0);

            $lineCapacity = (float) ($row['line_capacity'] ?? 0);
            $dailyHourlyQuantity = (float) ($row['hourly_quantity'] ?? 0);

            $row['category'] = $row['category'] ?? '-';
            $row['daily_hourly_quantity'] = [];
            $row['daily_target_dynamic'] = [];
            $row['produced_quantity'] = [];

            $key = $row['plant_id'].'_'.$row['line_id'].'_'.$row['item_id'];

            foreach ($dates as $date) {

                // Skip leave dates fast
                if (isset($leaveDates) && in_array($date, $leaveDates)) {
                    $row['daily_hourly_quantity'][$date] = '-';
                    $row['daily_target_dynamic'][$date] = '-';
                    $row['produced_quantity'][$date] = '-';
                    continue;
                }

                $todayTarget = $remainingDays > 0
                    ? round($remainingQty / $remainingDays, 2)
                    : 0;

                $producedQty = $producedData[$key][$date]->total_qty ?? 0;

                $row['daily_target_dynamic'][$date] = $todayTarget;
                $row['produced_quantity'][$date] = $producedQty;
                $row['daily_hourly_quantity'][$date] = $dailyHourlyQuantity * 22.5;

                // Carry forward remaining qty
                $remainingQty = max(0, $remainingQty - $producedQty);

                if ($remainingDays > 0) {
                    $remainingDays--;
                }
            }

            $records[] = $row;
        }

        $this->records = $records;
    }


    // public function loadProductionDataCategory($year, $month, $plantId, $lineId, $category)
    // {
    //     if (!$plantId || !$lineId || !$month || !$year || !$category) {
    //         $this->records = [];
    //         $this->dates = [];
    //         $this->leaveDates = [];
    //         return;
    //     }

    //     $dates = $this->getMonthDates($month, $year);
    //     $this->dates = $dates;

    //     $plans = ProductionPlan::query()
    //         ->join('items', 'items.id', '=', 'production_plans.item_id')
    //         ->join('lines', 'lines.id', '=', 'production_plans.line_id')
    //         ->join('plants', 'plants.id', '=', 'production_plans.plant_id')
    //         ->where('production_plans.plant_id', $plantId)
    //         ->where('production_plans.line_id', $lineId)
    //         ->whereMonth('production_plans.created_at', $month)
    //         ->whereYear('production_plans.created_at', $year)
    //         ->select(
    //             'production_plans.item_id',
    //             'production_plans.plant_id',
    //             'production_plans.line_id',
    //             'production_plans.plan_quantity',
    //             'production_plans.working_days',
    //             'production_plans.leave_dates',
    //             'items.code as item_code',
    //             'items.description as item_description',
    //             'lines.name as line_name',
    //             'lines.line_capacity as line_capacity',
    //             'plants.name as plant_name'
    //         )
    //         ->get();

    //     $leaveDates = [];

    //     if ($plans->isNotEmpty() && $plans[0]->leave_dates) {
    //         $leaveDates = array_map('trim', explode(',', $plans[0]->leave_dates));
    //     }

    //     $this->leaveDates = $leaveDates;

    //     $producedData = ProductionQuantity::selectRaw("
    //             plant_id,
    //             line_id,
    //             item_id,
    //             DATE(created_at) as prod_date,
    //             COUNT(*) as total_qty
    //         ")
    //         ->where('plant_id', $plantId)
    //         ->where('line_id', $lineId)
    //         ->whereMonth('created_at', $month)
    //         ->whereYear('created_at', $year)
    //         ->groupBy('plant_id', 'line_id', 'item_id', DB::raw('DATE(created_at)'))
    //         ->get()
    //         ->groupBy(fn($row) =>
    //             $row->plant_id . '_' . $row->line_id . '_' . $row->item_id
    //         )
    //         ->map(fn($group) => $group->keyBy('prod_date'));


    //     $records = [];

    //     foreach ($plans as $plan) {

    //         $row = $plan->toArray();

    //         $remainingQty  = (float) $row['plan_quantity'];
    //         $remainingDays = (int) ($row['working_days'] ?? 0);

    //         $lineCapacity = (float) ($row['line_capacity'] ?? 0);
    //         $dailyLineCapacity = (float) ($row['line_capacity'] ?? 0);


    //         $row['daily_line_capacity'] = [];
    //         $row['daily_target_dynamic'] = [];
    //         $row['produced_quantity'] = [];

    //         $key = $row['plant_id'].'_'.$row['line_id'].'_'.$row['item_id'];

    //         foreach ($dates as $date) {

    //             // Skip leave dates fast
    //             if (isset($leaveDates) && in_array($date, $leaveDates)) {
    //                 $row['daily_line_capacity'][$date] = '-';
    //                 $row['daily_target_dynamic'][$date] = '-';
    //                 $row['produced_quantity'][$date] = '-';
    //                 continue;
    //             }

    //             $todayTarget = $remainingDays > 0
    //                 ? round($remainingQty / $remainingDays, 2)
    //                 : 0;

    //             $producedQty = $producedData[$key][$date]->total_qty ?? 0;

    //             $row['daily_target_dynamic'][$date] = $todayTarget;
    //             $row['produced_quantity'][$date] = $producedQty;
    //             $row['daily_line_capacity'][$date] = $dailyLineCapacity;

    //             // Carry forward remaining qty
    //             $remainingQty = max(0, $remainingQty - $producedQty);

    //             if ($remainingDays > 0) {
    //                 $remainingDays--;
    //             }
    //         }

    //         $records[] = $row;
    //     }

    //     $this->records = $records;
    // }


    public function exportProductionData()
    {
        return Excel::download(
            new ProductionPlanExport($this->records, $this->dates),
            'production_plan_data.xlsx'
        );
    }

    public function render()
    {
        // return view('livewire.production-target-plan');
        return view('livewire.production-target-plan', [
            'records' => $this->records,
            'dates'   => $this->dates,
        ]);
    }
}
