<?php

namespace App\Http\Controllers;

use App\Models\SocialAccount;
use App\Models\ActivityLog;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class SocialAccountController extends Controller
{
    public function index()
    {
        $accounts = auth()->user()->socialAccounts()->orderBy('created_at', 'desc')->get();
        return view('accounts.index', compact('accounts'));
    }

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

    public function store(Request $request)
    {
        $validated = $request->validate([
            'platform_name' => ['required', Rule::in(['facebook', 'instagram', 'twitter', 'linkedin'])],
            'account_name' => ['required', 'string', 'max:255'],
            'access_token' => ['nullable', 'string', 'max:2000'],
            'status' => ['required', Rule::in(['active', 'inactive'])],
        ]);

        $account = auth()->user()->socialAccounts()->create($validated);

        ActivityLog::create([
            'user_id' => auth()->id(),
            'action' => 'create_social_account',
            'model_type' => SocialAccount::class,
            'model_id' => $account->id,
            'description' => "Created {$validated['platform_name']} account: {$validated['account_name']}",
            'ip_address' => $request->ip(),
        ]);

        return redirect()->route('accounts.index')
            ->with('success', 'Social account added successfully.');
    }

    public function edit(SocialAccount $account)
    {
        $this->authorize('update', $account);
        return view('accounts.edit', compact('account'));
    }

    public function update(Request $request, SocialAccount $account)
    {
        $this->authorize('update', $account);

        $validated = $request->validate([
            'platform_name' => ['required', Rule::in(['facebook', 'instagram', 'twitter', 'linkedin'])],
            'account_name' => ['required', 'string', 'max:255'],
            'access_token' => ['nullable', 'string', 'max:2000'],
            'status' => ['required', Rule::in(['active', 'inactive'])],
        ]);

        $account->update($validated);

        ActivityLog::create([
            'user_id' => auth()->id(),
            'action' => 'update_social_account',
            'model_type' => SocialAccount::class,
            'model_id' => $account->id,
            'description' => "Updated {$validated['platform_name']} account: {$validated['account_name']}",
            'ip_address' => $request->ip(),
        ]);

        return redirect()->route('accounts.index')
            ->with('success', 'Social account updated successfully.');
    }

    public function destroy(SocialAccount $account)
    {
        $this->authorize('delete', $account);

        ActivityLog::create([
            'user_id' => auth()->id(),
            'action' => 'delete_social_account',
            'model_type' => SocialAccount::class,
            'model_id' => $account->id,
            'description' => "Deleted {$account->platform_name} account: {$account->account_name}",
            'ip_address' => request()->ip(),
        ]);

        $account->delete();

        return redirect()->route('accounts.index')
            ->with('success', 'Social account deleted successfully.');
    }
}
