<?php

namespace App\Http\Controllers;

use App\Models\OperationDetails;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class OperationDetailsController extends Controller
{
    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        return view('operation-details.form');
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $validated = $request->validate([
            'supplying_companies' => 'nullable|string',
            'harvest_islands' => 'nullable|string',
            'target_species' => 'nullable|string',
            'fishing_grounds' => 'nullable|string',
            'fishing_gears_methods' => 'nullable|string',
            'processing_methods' => 'nullable|string',
            'number_of_fishing_days' => 'nullable|integer|min:0',
            'business_plan_path' => 'nullable|file|mimes:pdf,jpg,jpeg,png|max:5120',
        ]);

        // Handle file upload
        if ($request->hasFile('business_plan_path')) {
            $validated['business_plan_path'] = $request->file('business_plan_path')
                ->store('operation-documents/business-plans', 'public');
        }

        // Add authenticated user ID
        $validated['user_id'] = auth()->id();

        OperationDetails::create($validated);

        return redirect()
            ->route('applicant.dashboard')
            ->with('success', 'Operation details saved! Please complete Vessel Details next.');
    }

    /**
     * Show the form for editing the specified resource.
     */
    // ✅ CORRECT
    public function edit(OperationDetails $operationDetails)
    {
        if ($operationDetails->user_id !== auth()->id()) {
            return redirect()->route('applicant.dashboard')
                ->with('error', 'Unauthorized access.');
        }
        
        return view('operation-details.edit', compact('operationDetails'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, OperationDetails $operationDetails)
    {
        // Check if user owns this record
        if ($operationDetails->user_id !== auth()->id()) {
            return redirect()->route('applicant.dashboard')
                ->with('error', 'Unauthorized access.');
        }

        $validated = $request->validate([
            'supplying_companies' => 'nullable|string',
            'harvest_islands' => 'nullable|string',
            'target_species' => 'nullable|string',
            'fishing_grounds' => 'nullable|string',
            'fishing_gears_methods' => 'nullable|string',
            'processing_methods' => 'nullable|string',
            'number_of_fishing_days' => 'nullable|integer|min:0',
            'business_plan_path' => 'nullable|file|mimes:pdf,jpg,jpeg,png|max:5120',
        ]);

        // Handle file upload
        if ($request->hasFile('business_plan_path')) {
            // Delete old file if exists
            if ($operationDetails->business_plan_path) {
                Storage::disk('public')->delete($operationDetails->business_plan_path);
            }
            
            $validated['business_plan_path'] = $request->file('business_plan_path')
                ->store('operation-documents/business-plans', 'public');
        }

        $operationDetails->update($validated);

        return redirect()
            ->route('applicant.dashboard')
            ->with('success', 'Operation details updated successfully!');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(OperationDetails $operationDetails)
    {
        // Check if user owns this record
        if ($operationDetails->user_id !== auth()->id()) {
            return redirect()->route('applicant.dashboard')
                ->with('error', 'Unauthorized access.');
        }

        // Delete associated file
        if ($operationDetails->business_plan_path) {
            Storage::disk('public')->delete($operationDetails->business_plan_path);
        }

        $operationDetails->delete();

        return redirect()
            ->route('applicant.dashboard')
            ->with('success', 'Operation details deleted successfully.');
    }
}