<?php

namespace App\Http\Controllers;

use App\Models\TimeEntry;
use App\Models\StaffTaskType;
use App\Models\Job;
use Illuminate\Http\Request;
use Carbon\Carbon;

class TimesheetController extends Controller
{
    public function index()
    {
        $user     = auth()->user();
        $isTrades = $user->role?->name === 'trades';
        $isProcessor = $user->role?->name === 'processor';
        $isEmployee = $user->role?->name === 'factory_employee';

        if($isProcessor){
            $taskTypes = StaffTaskType::where('is_active', true)->where('task_category','processor')->orderBy('name')->get();
        }elseif($isEmployee){
            $taskTypes = StaffTaskType::where('is_active', true)->where('task_category','employee')->orderBy('name')->get();
        }else{
            $taskTypes = StaffTaskType::where('is_active', true)->orderBy('name')->get();
        }

        

        // Trades users: only see their assigned jobs that are NOT completed
        // Other users: see all active jobs
        if ($isTrades) {
            $jobs = $user->assignedJobs()
                ->where('status', '!=', 'completed')
                ->orderBy('job_number')
                ->get();
        }elseif($isProcessor){
            $jobs =Job::with(['contact', 'consultant', 'manager'])->whereHas('checkMeasures', fn($q) => $q->where('status', 'completed'))->where('stage','processing')->orderBy('job_number', 'desc')->get();
        }elseif($isEmployee){
            $jobs =Job::with(['contact', 'consultant', 'manager'])->whereHas('checkMeasures', fn($q) => $q->where('status', 'completed'))->where('stage','processing')->orderBy('job_number', 'desc')->get();
        } else {
            $jobs = Job::whereIn('status', ['active'])->orderBy('job_number')->get();
        }

        // Running timer (if any)
        $running = TimeEntry::where('user_id', $user->id)
            ->where('status', 'running')
            ->with('taskType', 'job')
            ->first();

        // Recent entries
        $entries = TimeEntry::where('user_id', $user->id)
            ->with('taskType', 'job')
            ->orderByDesc('started_at')
            ->paginate(20);

        // Weekly summary
        $weekStart    = Carbon::now()->startOfWeek();
        $weeklyEntries = TimeEntry::where('user_id', $user->id)
            ->where('started_at', '>=', $weekStart)
            ->where('status', '!=', 'running')
            ->get();
        $weeklyHours    = $weeklyEntries->sum('duration_minutes') / 60;
        $weeklyEarnings = $weeklyHours * ($user->hourly_rate ?? 0);

        return view('timesheet.index', compact(
            'taskTypes', 'jobs', 'running', 'entries', 'weeklyHours', 'weeklyEarnings'
        ));
    }

    public function start(Request $request)
    {
        $user     = auth()->user();
        $isTrades = $user->role?->name === 'trades';

        // Stop any running timer first
        TimeEntry::where('user_id', $user->id)->where('status', 'running')->each(function ($entry) {
            $mins = $entry->started_at->diffInMinutes(now());
            $entry->update([
                'stopped_at'       => now(),
                'duration_minutes' => $mins,
                'status'           => 'completed',
            ]);
        });

        $data = $request->validate([
            'task_type_id' => $isTrades ? 'nullable|exists:staff_task_types,id' : 'required|exists:staff_task_types,id',
            'job_id'       => 'required|exists:crm_jobs,id',
            'notes'        => 'nullable|string|max:500',
        ], [
            'task_type_id.required' => 'Please select a task type before starting the timer.',
            'job_id.required'       => 'Please select a job before starting the timer.',
        ]);

        // Trades users: validate the job is actually assigned to them
        if ($isTrades) {
            $assigned = $user->assignedJobs()
                ->where('crm_jobs.id', $data['job_id'])
                ->exists();
            if (! $assigned) {
                return back()->with('error', 'You can only start a timer for jobs assigned to you.');
            }

            // Also verify the job is not completed
            $job = Job::findOrFail($data['job_id']);
            if ($job->status === 'completed') {
                return back()->with('error', 'Cannot start a timer for a completed job.');
            }
        }

        TimeEntry::create([
            'user_id'      => $user->id,
            'task_type_id' => $data['task_type_id'] ?? null,
            'job_id'       => $data['job_id'] ?? null,
            'started_at'   => now(),
            'status'       => 'running',
            'notes'        => $data['notes'] ?? null,
        ]);

        return back()->with('success', 'Timer started.');
    }

    public function stop(Request $request)
    {
        $user  = auth()->user();
        $entry = TimeEntry::where('user_id', $user->id)->where('status', 'running')->first();
        if ($entry) {
            $mins = $entry->started_at->diffInMinutes(now());
            $entry->update([
                'stopped_at'       => now(),
                'duration_minutes' => $mins,
                'status'           => 'completed',
            ]);
            return back()->with('success', 'Timer stopped. ' . $entry->elapsed . ' recorded.');
        }
        return back()->with('error', 'No running timer found.');
    }

    public function destroy(TimeEntry $entry)
    {
        $user = auth()->user();
        if ($entry->user_id !== $user->id && $user->role?->name !== 'office_admin') {
            abort(403);
        }
        $entry->delete();
        return back()->with('success', 'Entry deleted.');
    }

    // Admin: view all timesheets
    public function admin()
    {
        $entries = TimeEntry::with('user', 'taskType', 'job')
            ->orderByDesc('started_at')
            ->paginate(50);

        $users = \App\Models\User::with('role')
            ->whereHas('role', fn($q) => $q->whereIn('name', ['factory_employee', 'trades', 'processor']))
            ->orderBy('name')
            ->get();

        return view('timesheet.admin', compact('entries', 'users'));
    }
}
