import { FileUploadInput } from '@admin/components/ui/forms/file-upload-input';
import { FormGroup } from '@admin/components/ui/forms/form-group';
import { FormNumberInput } from '@admin/components/ui/forms/form-number-input';
import { FormSection } from '@admin/components/ui/forms/form-section';
import { FormSwitch } from '@admin/components/ui/forms/form-switch';
import { MultiFileUploadInput } from '@admin/components/ui/forms/multi-file-upload-input';

export type ConferenceHallFormData = {
    name: string;
    summary: string;
    description: string;
    capacity: number | string;
    price_per_head: string;
    featured_image: string;
    gallery: string[];
    inclusions: string;
    details: string;
    notes: string;
    opens_at: string;
    closes_at: string;
    status: boolean;
};

type ConferenceHallFormFieldsProps = {
    data: ConferenceHallFormData;
    errors: Partial<Record<keyof ConferenceHallFormData, string>>;
    setData: (key: string, value: unknown) => void;
    tzsPerUsd: string | number | null;
};

function mediaUrl(path: string): string {
    if (path.startsWith('/') || path.startsWith('http')) {
        return path;
    }

    return `/storage/${path}`;
}

function usdEquivalent(amount: string, tzsPerUsd: string | number | null): string {
    const rate = Number(tzsPerUsd);
    const value = Number(amount);

    if (!rate || !value) {
        return 'Stored in Tanzanian shillings. The public total is this rate multiplied by the number of guests.';
    }

    const usd = value / rate;

    return `About USD ${usd.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })} per guest at the current rate.`;
}

export function ConferenceHallFormFields({ data, errors, setData, tzsPerUsd }: ConferenceHallFormFieldsProps) {
    return (
        <>
            <FormSection title="Hall">
                <FormGroup
                    label="Name"
                    error={errors.name}
                    inputProps={{
                        value: data.name,
                        onChange: (event) => setData('name', event.target.value),
                        required: true,
                    }}
                />
                <FormGroup
                    as="textarea"
                    label="Summary"
                    error={errors.summary}
                    inputProps={{
                        value: data.summary,
                        rows: 3,
                        onChange: (event) => setData('summary', event.target.value),
                    }}
                />
                <FormGroup
                    as="textarea"
                    label="Description"
                    error={errors.description}
                    inputProps={{
                        value: data.description,
                        rows: 6,
                        onChange: (event) => setData('description', event.target.value),
                    }}
                />
                <FormSwitch
                    label="Published on the site"
                    checked={data.status}
                    onChange={(event) => setData('status', event.target.checked)}
                />
            </FormSection>

            <FormSection title="Price and capacity">
                <FormNumberInput
                    label="Capacity"
                    helpText="Maximum guests. Leave blank if you do not want a limit. The booking form will not accept more than this."
                    min={1}
                    value={data.capacity}
                    error={errors.capacity}
                    onChange={(value) => setData('capacity', value)}
                />
                <FormGroup
                    label="Price per guest (TZS)"
                    helpText={usdEquivalent(data.price_per_head, tzsPerUsd)}
                    error={errors.price_per_head}
                    inputProps={{
                        value: data.price_per_head,
                        inputMode: 'decimal',
                        onChange: (event) => setData('price_per_head', event.target.value),
                        required: true,
                    }}
                />
            </FormSection>

            <FormSection title="Hours">
                <FormGroup
                    label="Opens"
                    helpText="Guests can only request a start and end inside this window. The day overview uses the same hours."
                    error={errors.opens_at}
                    inputProps={{
                        type: 'time',
                        value: data.opens_at,
                        onChange: (event) => setData('opens_at', event.target.value),
                        required: true,
                    }}
                />
                <FormGroup
                    label="Closes"
                    error={errors.closes_at}
                    inputProps={{
                        type: 'time',
                        value: data.closes_at,
                        onChange: (event) => setData('closes_at', event.target.value),
                        required: true,
                    }}
                />
            </FormSection>

            <FormSection title="Photos">
                <FileUploadInput
                    label="Featured image (830×630 px recommended)"
                    helpText="Used when the gallery is empty."
                    inputId="conference-hall-featured"
                    uploadUrl="/admin/api/conference_hall/upload_featured"
                    accept="image/jpeg,image/png,image/jpg,image/webp"
                    previewUrl={data.featured_image ? mediaUrl(data.featured_image) : ''}
                    previewType="image"
                    error={errors.featured_image}
                    onSuccess={(filename) => setData('featured_image', filename)}
                />
                <MultiFileUploadInput
                    label="Gallery (830×630 px recommended)"
                    helpText="Photos on the conference hall page. Up to 12."
                    inputId="conference-hall-gallery"
                    uploadUrl="/admin/api/conference_hall/upload_gallery"
                    paths={data.gallery}
                    error={errors.gallery}
                    onChange={(paths) => setData('gallery', paths)}
                />
            </FormSection>

            <FormSection title="What the hall includes">
                <FormGroup
                    as="textarea"
                    label="Inclusions"
                    helpText="One item per line. Optional icon after a pipe, for example Projector|fa-solid fa-display."
                    error={errors.inclusions}
                    inputProps={{
                        value: data.inclusions,
                        rows: 6,
                        onChange: (event) => setData('inclusions', event.target.value),
                    }}
                />
                <FormGroup
                    as="textarea"
                    label="Extra details"
                    helpText="One fact per line, as Label|Value. These appear beside the description."
                    error={errors.details}
                    inputProps={{
                        value: data.details,
                        rows: 5,
                        onChange: (event) => setData('details', event.target.value),
                    }}
                />
                <FormGroup
                    as="textarea"
                    label="Notes"
                    helpText="One note per line. Shown on the hall page."
                    error={errors.notes}
                    inputProps={{
                        value: data.notes,
                        rows: 5,
                        onChange: (event) => setData('notes', event.target.value),
                    }}
                />
            </FormSection>
        </>
    );
}
