<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
use App\Models\PersonnelActionForm;
use App\Models\Employee;

class Analytics extends Model
{
    // Computed model only — no actual table
    protected $table = null;
    public $timestamps = false;

    /**
     * Get Regularization trend for the current month.
     *
     * @param int|null $companyId
     * @return array
     */
    public static function getNewRegularization($companyId = null)
    {
        $now = Carbon::now();
        $currentMonthStart = $now->copy()->startOfMonth();
        $currentMonthEnd = $now->copy()->endOfMonth();
        $previousMonthStart = $now->copy()->subMonth()->startOfMonth();
        $previousMonthEnd = $now->copy()->subMonth()->endOfMonth();

        // Base query
        $query = PersonnelActionForm::query()
            ->whereNotNull('approved_at')
            ->whereNotNull('effectivity')
            ->where('nature_of_request_id', 13); // Regularization

        if ($companyId) {
            $query->where('company_id', $companyId);
        }

        // Counts
        $current = (clone $query)
            ->whereBetween('effectivity', [$currentMonthStart, $currentMonthEnd])
            ->count();

        $previous = (clone $query)
            ->whereBetween('effectivity', [$previousMonthStart, $previousMonthEnd])
            ->count();

        [$change, $label] = self::percentageAndLabel($previous, $current);

        return [
            'type' => 'Regularization',
            'month' => $now->format('F Y'),
            'count' => $current,
            'change' => $change,
            'label' => $label,
        ];
    }

    /**
     * Get Promotion trend for the current month.
     *
     * @param int|null $companyId
     * @return array
     */
    public static function getNewPromotion($companyId = null)
    {
        $now = Carbon::now();
        $currentMonthStart = $now->copy()->startOfMonth();
        $currentMonthEnd = $now->copy()->endOfMonth();
        $previousMonthStart = $now->copy()->subMonth()->startOfMonth();
        $previousMonthEnd = $now->copy()->subMonth()->endOfMonth();

        // Base query
        $query = PersonnelActionForm::query()
            ->whereNotNull('approved_at')
            ->whereNotNull('effectivity')
            ->where('nature_of_request_id', 1); // Promotion

        if ($companyId) {
            $query->where('company_id', $companyId);
        }

        // Counts
        $current = (clone $query)
            ->whereBetween('effectivity', [$currentMonthStart, $currentMonthEnd])
            ->count();

        $previous = (clone $query)
            ->whereBetween('effectivity', [$previousMonthStart, $previousMonthEnd])
            ->count();

        [$change, $label] = self::percentageAndLabel($previous, $current);

        return [
            'type' => 'Promotion',
            'month' => $now->format('F Y'),
            'count' => $current,
            'change' => $change,
            'label' => $label,
        ];
    }

    /**
     * Get Newly Hired Employees trend for the current month.
     *
     * @param int|null $companyId
     * @return array
     */
    public static function getNewHire($companyId = null)
    {
        $now = Carbon::now();
        $currentMonthStart = $now->copy()->startOfMonth();
        $currentMonthEnd = $now->copy()->endOfMonth();
        $previousMonthStart = $now->copy()->subMonth()->startOfMonth();
        $previousMonthEnd = $now->copy()->subMonth()->endOfMonth();

        // Base query for employees
        $query = Employee::query()
            ->whereNotNull('date_hired');

        if ($companyId) {
            $query->where('company_id', $companyId);
        }

        // Counts
        $current = (clone $query)
            ->whereBetween('date_hired', [$currentMonthStart, $currentMonthEnd])
            ->count();

        $previous = (clone $query)
            ->whereBetween('date_hired', [$previousMonthStart, $previousMonthEnd])
            ->count();

        [$change, $label] = self::percentageAndLabel($previous, $current);

        return [
            'type' => 'Newly Hired',
            'month' => $now->format('F Y'),
            'count' => $current,
            'change' => $change,
            'label' => $label,
        ];
    }

    public static function getNewClearance($companyId = null)
    {
        $now = Carbon::now();
        $currentMonthStart = $now->copy()->startOfMonth();
        $currentMonthEnd = $now->copy()->endOfMonth();
        $previousMonthStart = $now->copy()->subMonth()->startOfMonth();
        $previousMonthEnd = $now->copy()->subMonth()->endOfMonth();

        $query = Clearance::query()
            ->whereNotNull('deleted_at') // only count soft-deleted
            ->whereNotNull('created_at');

        if ($companyId) {
            $query->where('company_id', $companyId);
        }

        $current = (clone $query)
            ->whereBetween('created_at', [$currentMonthStart, $currentMonthEnd])
            ->count();

        $previous = (clone $query)
            ->whereBetween('created_at', [$previousMonthStart, $previousMonthEnd])
            ->count();

        [$change, $label] = self::percentageAndLabel($previous, $current);

        return [
            'type' => 'New Clearance',
            'month' => $now->format('F Y'),
            'count' => $current,
            'change' => $change,
            'label' => $label,
        ];
    }

    /**
     * Compute percentage and label for change.
     *
     * @param int $previous
     * @param int $current
     * @return array [percentage, label]
     */
    protected static function percentageAndLabel($previous, $current)
    {
        if ($previous == 0 && $current == 0) {
            return [0, 'No Change'];
        }

        if ($previous == 0) {
            return [$current > 0 ? 100 : 0, $current > 0 ? 'Increase' : 'No Change'];
        }

        $change = round((($current - $previous) / $previous) * 100, 2);

        if ($change > 0) {
            $label = 'Increase';
        } elseif ($change < 0) {
            $label = 'Decrease';
        } else {
            $label = 'No Change';
        }

        return [$change, $label];
    }
}
