<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Models\Contract;
use App\Models\Contractor;
use Illuminate\Http\Request;

class ContractController extends Controller
{
    public function index()
    {
        $contracts = Contract::with(['jobPackage', 'contractor'])
            ->orderBy('created_at', 'desc')
            ->paginate(10);
        return view('contracts.index', compact('contracts'));
    }

    public function create()
    {
        return view('contracts.create');
    }

    public function store(Request $request)
    {
        return redirect()->route('ppk.contracts.index');
    }

    public function show($id)
    {
        $contract = Contract::with(['jobPackage', 'contractor'])->findOrFail($id);
        return view('contracts.show', compact('contract'));
    }

    public function edit($id)
    {
        $contract = Contract::findOrFail($id);
        return view('contracts.edit', compact('contract'));
    }

    public function update(Request $request, $id)
    {
        return redirect()->route('ppk.contracts.index');
    }

    public function destroy($id)
    {
        return redirect()->route('ppk.contracts.index');
    }

    public function myContracts()
    {
        $user = auth()->user();
        $contractor = Contractor::where('user_id', $user->id)->first();
        
        $contracts = $contractor ? Contract::where('contractor_id', $contractor->id)
            ->with(['jobPackage'])
            ->orderBy('created_at', 'desc')
            ->get() : collect();
        
        return view('contractor.my-contracts', compact('contracts'));
    }
}