<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Farm;
use App\Models\Farm_section;
use Illuminate\Support\Facades\Auth;

class FarmController extends Controller
{
    // Display all farms
    public function index()
    {
        $user = Auth::user(); // Logged-in user
        $roleId = $user->id_role; // Access the role ID from the Login model

        // Fetch farms based on the logged-in user's role
        $farms = Farm::with('farm_section')
            ->when($roleId == 1, function ($query) {
                // Superadmin sees all farms
                return $query;
            })
            ->when($roleId == 2, function ($query) use ($user) {
                // Manager sees only farms they created
                return $query->where('login_id', $user->id);
            })
            ->when($roleId == 3, function ($query) use ($user) {
                // Farmer sees only their own farm (use a relationship or explicit field to identify ownership)
                return $query->where('login_id', $user->id); // Adjust 'owner_id' to the correct column in the `farms` table.
            })
            ->orderBy('created_at', 'desc')
            ->paginate(10);

        return view('farms.farm-index', compact('farms'));
    }


    // Show form to create a new farm
    public function create()
    {
        return view('farms.farm-create');
    }

    // Store a new farm
    public function store(Request $request)
    {
        $user = Auth::user();
        $request->validate([
            'address' => 'required|string|max:255',
            'postcode' => 'required|string|max:10',
            'registration_no' => 'required|string|unique:farm,registration_no'
        ]);

        Farm::create([

            'address' => $request->address,
            'postcode' => $request->postcode,
            'registration_no' => $request->registration_no,
            'login_id' => $user->id,


        ]);

        return redirect()->route('farms.index')->with('success', 'Farm created successfully.');
    }

    // Show form to edit an existing farm
    public function edit(Farm $farm)
    {
        return view('farms.farm-edit', compact('farm'));
    }

    // Update an existing farm
    public function update(Request $request, Farm $farm)
    {
        $request->validate([
            'address' => 'required|string|max:255',
            'postcode' => 'required|string|max:10',
            'registration_no' => 'required|string|unique:farm,registration_no,' . $farm->id
        ]);

        $farm->update($request->all());

        return redirect()->route('farms.index')->with('success', 'Farm updated successfully.');
    }

    // Delete a farm
    public function destroy(Farm $farm)
    {
        $farm->delete();

        return redirect()->route('farms.index')->with('success', 'Farm deleted successfully.');
    }

    // Show form to create a new farm section for a specific farm
    public function createSection(Farm $farm)
    {
        return view('farms.create-section', compact('farm'));
    }

    // Store a new farm section
    public function storeSection(Request $request, Farm $farm)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'registration_no' => 'required|string|unique:farm_section,registration_no'
        ]);

        $farm->farm_section()->create($request->all());

        return redirect()->route('farms.index')->with('success', 'Farm section created successfully.');
    }

    public function getSections(Farm $farm)
    {
        $sections = $farm->farm_section; // Assuming 'farm_section' relationship in Farm model
        return response()->json($sections);
    }
}
